How do I get images in certain fixed size, example I've attached the image and codes for your review
Please click on this link (image) to understand my question
<img src="<?php echo $picfile; ?>" border="0" width="<?php echo $imgsize[0]; ?>" height="<?php echo $imgsize[1]; ?>" align="left" style="border:1px solid black;margin-right:5px;">
You can get image details with
$details = get_image_size($filename).
$details[3] will contain the width and height in html format ready for you to use.
Could possibly try this
img src='$filename' style='width:250px; height: 250px'
allows you to strech image to specific size
to scale you could use
img src='$filename' style='width:auto; height: 250px'
There are a few ways to do this. As you are looping through your images, you want to keep track of the maximum width.
$newImageArray = array();
$maxWidth = 0;
$maxHeight = 0;
$i = 0;
forEach ( $imageArray as $picfile ) {
$newImageArray[$i]['picFile'] = $picfile;
$imgsize = get_image_size($picfile);
$newImageArray[$i]['width'] = $imgsize[0];
if ( $imgsize[0] > $maxWidth ) {
$maxWidth = $imgsize[0];
}
$newImageArray[$i]['height'] = $imgsize[1];
if ( $imgsize[1] > $maxHeight ) {
$maxHeight = $imgsize[1];
}
$1++;
}
forEach ( $newImageArray as $i) { ?>
<img src="<?php echo $picfile; ?>" border="0" width="<?php echo $maxWidth; ?>" height="<?php echo $maxHeight; ?>" align="left" style="border:1px solid black;margin-right:5px;">
<?php
}
Now you shouldn't really be doing this. A CSS based option would work better. You can add a wrapper container with a width on it and set the images to display:block;width:100%; and the images will always fill the space. I will edit with that solution shortly.
This will keep the image within a fixed width, and scale the image to fit inside of the box when it is too large.
HTML:
<div class="imgWrap">
<img src="http://www.captainangry.com/img/1236647133510anonib.jpg">
</div>
CSS:
.imgWrap {
width:100px;
height:100px;
float:left;
}
.imgWrap img {
display:block;
max-width:100%;
}
If you don't want to do any work in PHP then just wrap the image in a div and set overflow:hidden. This assumes that you know the height you want all of your images to be.
<div style="width:<?php echo $imgsize[0]; ?>; height:100px; overflow:hidden; border:1px solid black; margin-right:5px">
<img src="<?php echo $picfile; ?>" width="<?php echo $imgsize[0]; ?>" height="<?php echo $imgsize[1]; ?>">
</div>
Related
Hi I've this simple php script for showing images from a folder.My problem is that images are displayed very closely .I want to add some space between images and border for each image.Pls help me.
$files = glob("images/gallery/thumb/*.*");
for ($i=1; $i<count($files); $i++)
{
$image = $files[$i];
echo '<img src="'.$image .'" alt="Random image" />';
}
Or, if you want a sort of grid:
$files = glob("images/gallery/thumb/*.*");
$count = count($files);
for($i = 1; $i < $count; $i++){
$image = $files[$i];
echo '<img src="'.$image .'" alt="Random image" style="border:2px solid black; margin: 5px; float: left;" />';
}
Use plain HTML line break <br/> for the space between two images.
Use CSS style="border:2px solid black;" (change it to suit your need) for the border.
And I’ll suggest you to store count($files) in a variable; otherwise it’ll have to be evaluated in every iteration of your for loop, causing your script to run slower.
Now your entire code will be something like the following:
$files = glob("images/gallery/thumb/*.*");
$count = count($files);
for($i = 1; $i < $count; $i++){
$image = $files[$i];
echo '<img src="'.$image .'" alt="Random image" style="border:2px solid black;"/><br/>';
}
I would attach a class to to the images.
echo '<link rel="stylesheet" href="gallery.css" type="text/css"';
...
$files = glob("images/gallery/thumb/*.*");
foreach ($files as $image){
echo '<img src="'.$image .'" alt="Random image" class="galleryImage"/>';
}
and then create a .css file with the style for the class. This will allow you to easily change the style via .css without having to touch your php code, and will make your rendered html file slightly smaller.
gallery.css
img.galleryImage{
border: 2px solid #888888;
padding: 10px;
}
Can any body tell how to change the width & height of iframe in php.
in database i am saving below format.while i am displaying i want to fixed size.
this is the iframe content
<iframe width="400" height="225" src="http://www.youtube.com/embed/c7ct6pNOvEE?feature=oembed" frameborder="0" allowfullscreen></iframe>
Here width & height changes dynamically based on site.then how to convert the width to 500 & height to 350 in php
Thanks in advance
You should just use CSS
html, body, iframe {
height: 100%;
width: 100%;
}
Edit: Next time you should define your question PROPERLY and add a proper descriotion before you post it.
Anyway, since you have the sizes in the database you can simply use echo's to change the size. First you need to get the width and height from the database, then echo it in your HTML.
<?php
$iframe = html_entity_decode($post['url']);
$newWidth = 610;
$newHeight = 450;
?>
<iframe
src="<?php echo $iframe; ?>"
height="<?php echo $newHeight; ?>"
width="<?php echo $newWidth; ?>"
frameborder="0" allowfullscreen>
</iframe>
Edit v12351524274573523
$iframe = '<iframe width="400" height="225" src="youtube.com/embed/c7ct6pNOvEE?feature=oembed" frameborder="0" allowfullscreen></iframe>';
$src = html_entity_decode($post['url']);
$height = 450;
$width = 610;
// add autoplay
$src = $src . (strstr($src, '?') ? '&': '?') . 'autoplay=1';
$iframe = preg_replace('/src="(.*?)"/i', 'src="' . $src .'"', $iframe);
$iframe = preg_replace('/height="(.*?)"/i', 'height="' . $height .'"', $iframe);
$iframe = preg_replace('/width="(.*?)"/i', 'width="' . $width .'"', $iframe);
I have an image that has it's width greater than the height and it is still outputting using the second if statement in the jQuery. I do believe it might have something to do with the PHP sector outputting multiple image files. So I changed the id selector to a class selector, but it doesn't want to budge. Does anybody know what I am doing wrong? I have hit rock bottom on this problem.
<?php $userid = $this->session->userdata('userid');
$selectedId = $_REQUEST['id'];
$query1 = $this->db->query("SELECT * FROM userProfilePhotos p, userProfileAlbums a WHERE p.isAlbumCover = 'Yes' AND p.userid = a.userid AND a.userid = '{$selectedId}' AND a.albumId = p.albumId");
foreach($query1->result() as $row1) {?>
<script type="text/javascript">
$(document).ready(function() {
function heightOverWidth() {
return '<div style="width: 102px; height: 136px; border-radius: 3pt; background-color: white; border: 1px solid #C1C1C1; padding: 10pt">'+
'<img id="profileImg" alt="" height="130" src="<?=base_url().$row1->imgURI?>" width="100" />'+
'</div><span class="link-font3"><?=$row1->albumName?></span> <span class="font1"> - <?=$row1->albumDesc?></span>';
}
var img = $(".profileImg");
if (img.width() >= img.height()) {
alert("1");
} if (img.height() >= img.width()) {
$("#albumList").append(heightOverWidth());
}
});
</script>
<?php list($width, $height, $type, $attr) = getimagesize(base_url() . $row1->imgURI); ?>
<div id="albumList" style="padding: 10pt">
<img class="profileImg" style="display: none" alt="" height="<?=$height?>" width="<?=$width?>" src="<?=base_url().$row1->imgURI?>" />
</div>
<?php } ?>
Change the width() to attr('width') and same thing with height. This will read your width/height attributes instead of trying to get the actual width and height. Your solution does not work as an element with display:none doesn't have any visible dimensions.
By using the below code i displayed 9 images, but all images are align side by side, how to display the every 3 images in one row and another3 images in another row ?
<table align="center" border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<?php $sel = $db->query("select * from gallery order by gallery_cat_id asc limit 1,1");
while($row=mysql_fetch_array($sel)){ ?>
<tr><td align="left" valign="top"><table align="center" border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody><?php $sel1 = $db->query("SELECT DISTINCT ( i.gallery_album_id )
FROM mov_gallery_album AS a, mov_gallery_images AS i
WHERE a.gallery_album_id = i.gallery_album_id
AND a.gallery_cat_id =".$row['gallery_cat_id']."
ORDER BY a.gallery_album_id DESC
LIMIT 0 , 9 "); if(mysql_num_rows($sel1)>0){ ?>
</tr><tr>
<?php
while($row1=mysql_fetch_array($sel1)){
$dis1 = $db->getRow("select * from ".ALBUMS." where gallery_album_id=".$row1['gallery_album_id']." limit 0,1");
$dis2 = $db->getRow("select * from ".GALLERY." where gallery_id=".$dis1['gallery_id']." limit 0,1");
$dis3 = $db->getRow("select * from ".ALBUMSIMAGES." where gallery_album_id=".$row1['gallery_album_id']." limit 0,1");
$dis4 = $db->getRow("select * from ".GALLERYCATEGORY." where gallery_cat_id=".$dis2['gallery_cat_id']." limit 0,1");
?>
<td align="left" width="100%"><table border="0" cellpadding="5" cellspacing="0">
<tbody><tr><td align="center" height="8" valign="middle" width="80">
<div style="border:0px;clear:both;padding-bottom:100px;margin-left:-110px;">
<div class="image_stack1"><a href="<?php echo SITE; ?>album/<?php echo ucfirst($dis1['gallery_cat_id']); ?>/<?php echo ucfirst($dis1['gallery_id']); ?>/<?php echo ucfirst($row1['gallery_album_id']); ?>/">
<img id="photo3" src="<?php echo SITE; ?>uploads/gallery/<?php echo $dis4['folder']; ? >/<?php echo $dis2['folder']; ?>/<?php echo $dis1['folder']; ?>/thumb/<?php echo $dis3['image']; ?>"width="80" height="80">
<div class="namehover1"><?php echo substr(ucfirst($dis1['name']),0,13); ?></div>
</a></div></div></td></tr></tbody></table></td><?php } ?></tr><tr><td class="midtitle" align="center" valign="middle"> </td></tr>
<tr><td style="padding-right: 10px;" align="right" colspan="4"><a href="<?php echo SITE; ?>gallery/<?php echo ucfirst($row['gallery_cat_id']); ?>/" class="midtitle">
<img src="http://www.img./viewall.png" border="0"/>
</a></td></tr><?php } ?></tbody></table></td></tr><?php } ?></tbody></table>
It would be wise to use div rather than table.
Some lines of css would do the trick. You wont have to logically determined to move to next line after 3 images. Here's how you can do with this in css
Lets have a div container for the all the images.
<style>
#photo_wrapper {
width:600px;
}
.photo {
width:150px;
height:150px;
display:block;
float:left;
border:6px #c5d0d6 solid;
margin-right:5px;
margin-bottom:5px;
overflow:hidden;
}
</style>
<div id="photo_wrapper">
<img src="image.jpg" class="photo"/>
<img src="image.jpg" class="photo"/>
<img src="image.jpg" class="photo"/>
<img src="image.jpg" class="photo"/>
<img src="image.jpg" class="photo"/>
<img src="image.jpg" class="photo"/>
</div>
this is generally done with the modulo (remainder after division) operator:
foreach ($results as $nr => $row) {
if (($nr % 3) == 0) echo '<tr>';
...
if (($nr % 3) == 0) echo '</tr>';
}
The concept is simple, use a counter and each time this counter MOD 3 is zero , start a new line.Or just count until 3 and reset the counter.
$i = 1;
while( $data = ....)
{
if($i == 1)
{
echo "<tr>"; //new line
}
echo "<td> .... </td>";
if($i == 3)
{
$i = 1;
echo "</tr>";
}
else
{
$i++;
}
}
//IMPORTANT! PLEASE NOTICE TO THIS PART: (OUT OF THE LOOP)
if($i < 3)
echo "</tr>";
you can put 9 photos in a div, then fix the width of that div to make it enough for only 3 photos in a line. And set float:left; for img tag in CSS.
I am looking for some sort of function that will resize an image to 100x100, 200x200 etc but keep the aspect ratio of the uploaded image in tact.
My plan would be to use some sort of cropping, so to crop in the centre of the image after it's height or width reaches 100.
So my logic is a bit like this.
if ( $currentHeight > $currentWidth ) {
//resize width to 100 and crop top and bottom off so it is 100 high
}
elseif ( $currentWidth > $currentHeight ) {
//resize height to 100 and crop left and right off so it is 100 wide
}
In my mind the image would then be 100x100 and it wont look odd!
Does anyone know how I could do this in a tidy function??
You can also use pure css to crop images to fit specific dimensions using clip:rect:
<style>
/*Size of div that contains the dox*/
.crop{
float:left;
position:relative;
width:100px;
height:100px;
margin:0 auto;
border: 2px solid #474747;
}
.crop img{
margin:0;
position:absolute;
top:-25px;
left:-25px;
/* (Top,Right,Bottom,Left) */
clip:rect(25px 125px 125px 25px);
}
</style>
<div class="crop">
<img src="http://static.php.net/www.php.net/images/php.gif" width="200" title="" alt="" />
<div>
<br/>
<div class="crop">
<img src="http://cdn.sstatic.net/stackoverflow/img/sprites.png?v=5" title="" alt="" />
<div>
See In Action
If you want to crop central part, then I suppose logic for counting coordinates can be such:
1) Count horisontal coordinats of crop area:
$horizontal_center = $current_width/2;
$left = $horizontal_center - $new_width/2;
$right = $horizontal_center + $new_width/2;
2) Count vertical coordinates of crop area:
$vertical_center = $current_height/2;
$top = $vertical_center - $new_height/2;
$bottom = $vertical_center + $new_height/2;
here is my answer - ive written a imagemagick/php function to do this:
stackoverflow.com/questions/20927238/