The lameness of this script amazes me.
| SitePoint Sponsor |
The lameness of this script amazes me.
1) this can easily be replaced with simple html, but there is a problem with both scripts:
2) simple resizing will greatly reduce load time, driving customers away from your site. Thumbnails do a much better job of improving load time, even though they are a hassle.
How did this show up on the first page of my results? I want those 5 minutes of my life back.
This is probably the most narrow solution possible to this problem. Why does Google spit out dated (5+ years) pages for a query whose answer is better served with a more recent page? Kiddies, if you can build your own search engine with a better query analyzer, a billion-dollar market is waiting for you. Google is dead.
There are some error in your script.
<img src="images/sock001.jpg" <?php imageResize($mysock[0],
$mysock[1], 150); ?>>
Correction(put echo before imageresize command)
<img src="images/sock001.jpg" <?php echo imageResize($mysock[0],
$mysock[1], 150); ?>>


Hi etgen (and anyone else!)
The script above will work but will give you very poor image quality.
I have changed the script slightly to add imagecreatetruecolor instead of imagecreate.
To use this on more than one image, you will need to save it as a separate php file eg: thumbnail.php and call it in the <img src> tag
thumbnail.php
PHP Code:<?php
// File and new size
$filename = $_GET['img'];
$percent = 0.5;
// Content type
header('Content-type: image/jpeg');
// Get new sizes
list($width, $height) = getimagesize($filename);
$newwidth = $width * $percent;
$newheight = $height * $percent;
// Load
$source = imagecreatefromjpeg($filename);
$thumb = imagecreatetruecolor($newwidth, $newheight);
// Resize
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
// Output
imagejpeg($thumb);
?>
You pass the name of the image (and path) directly to the script and you can also easily change it to take a thumbnail width.
usage:
Code:<img src="simpleImgResize.php?img=images/myimage.jpg" /> <img src="simpleImgResize.php?img=images/myotherimage.jpg" />
HTH
Spike
Mike Swiffin - Community Team Leader
Only a woman can read between the lines of a one word answer.....
I started out with nothing... and still got most of it left!
Thanks a lot Spike! Works great!
Spikes works great, but I needed to specify with in pixels (not percentage) and maintain aspect ratio, additionally, not all thumbs would have the same with...(or I could reuse the script for other sites without editing -talk about lazy..tee hee-).
Here it is:
<?php
// File and new size
$filename = $_GET['img'];
// Content type
header('Content-type: image/jpeg');
// Get new sizes
list($width, $height) = getimagesize($filename);
//To specify width
$newwidth = $_GET['w'];
$newheight = ($height*$newwidth)/$width;
// Load
$source = imagecreatefromjpeg($filename);
$thumb = imagecreatetruecolor($newwidth, $newheight);
// Resize
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
// Output
imagejpeg($thumb);
?>
Usage:
<img src="thumbnails.php?w=150&img=../images/product/7.2.jpg" />
Best
Arturo
i wanna kiss you. but im not gay :) thanks so much :)
What should I do If I need to resize gif-animated picture? Is it possible in PHP?


Hi Rusk, Animated gif's cant be resized in PHP without being 'flattened' into a single image. Not sure if ImageMagick can retain the data or not but you might want to look into it as an option
Mike Swiffin - Community Team Leader
Only a woman can read between the lines of a one word answer.....
I started out with nothing... and still got most of it left!
Hello, I'm trying to use this script on a website I'm making. Can anyone help me via email maybe? When I upload the page with the script nothing shows...
I want to resize images as they are uplaoded, and make the actual image a new size like thumb_imagename etc. So, that then it is safe for use on the web. Is this possible with few modifactions for this script?
@ Daquan
Most likely the script is giving you an error and your server is set not to display errors. The script is giving you and error because when you just copy paste it in, it line breaks and doesn't keep the comment commented!
Make sure these three lines are also commented:
//formula accordingly...this is so this script will work
//dynamically with any size image
//can plug this function inside an image tag and just get the
Secondly use standard html comment tags!
Correct:
<!-- -->
Incorrect:
<!- -->
Does not output anything, please make one that actually works.
When the pictures get resized they look quite choppy even if u keep the aspect ratio.
Is there a way to fix that?


@Daquan ~ Check as the Anonymous Coward suggested, if it's still not playing - register and start a thread
@aUser ~ what doesnt output anything? The original script? the script I or Mendez wrote? Be specific!
@Adam Coppard ~ To save the image instead of serving it as a thumbnail, change the arguments on the imagejpeg function:
@Manuel ~ The images look choppy because the browser is doing the resizing and browsers aren't supposed to do that. The rendering engine in IE isnt designed to resize images but does the best it can.PHP Code:/* Save the thumbnail
**
** $thumb = generated image
** $filename = location of final image eg 'images/thumbnails/myThumb.jpg
** $quality = Quality of the thumbnail; 0 - 100 */
imagejpeg($thumb, $filename, $quality);
Either manually resize them in photoshop or use the thumbnailing script above![]()
Mike Swiffin - Community Team Leader
Only a woman can read between the lines of a one word answer.....
I started out with nothing... and still got most of it left!
Hi
I have "inherited" a web site to manage. This site has a problem with the image upload not creating thumbnails but the original designer left no comments as to how he wrote his code.
I think the problem lies in the following somewhere but I am at a loss as to exactly where. The site stores in a database both reduced size and thumbs for display on a single page.
Any helpful comments most appreciated.
PHP Code:<?php
if($_POST[sbmt]):
for($i=0;$i<count($_FILES[foto][tmp_name]);$i++):
if($_FILES[foto][tmp_name][$i]):
$r=mysql_query("insert into " . $prev . "property_pics(id) values (\"" . $_REQUEST[id] . "\")");
$pid=mysql_insert_id();
$e=explode(".",$_FILES[foto][name][$i]);
$ext=substr($_FILES[foto][name][$i],-3,3);
$size=$foto_size;
$ext=strtolower($e[1]);
if($ext=="peg"){$ext="jpg";}
copy($_FILES[foto][tmp_name][$i],"../pics/" . $pid . "." . $ext);
copy($_FILES[foto][tmp_name][$i],"../pics/thumb-" . $pid . "." . $ext);
mysql_query("update " . $prev . "property_pics set file='" . $pid . "." . $ext . "',thumb='thumb-" . $pid . "." . $ext . "' where pid=" . $pid);
$nw=145;$nh=133;
$img=$tpath . "/thumb-" . $pid . "." . $ext;
$dimensions = GetImageSize($img);
$thname = $img;
$w=$dimensions[0];
$h=$dimensions[1];
if($ext=="jpg"):
$img2 = ImageCreateFromJpeg($img);
elseif($ext=="gif"):
$img2 = ImageCreateFromGif($img);
elseif($ext=="png"):
$img2 = ImageCreateFromPng($img);
endif;
$thumb=ImageCreateTrueColor($nw,$nh);
$wm = $w/$nw;
$hm = $h/$nh;
$h_height = $nh/2;
$w_height = $nw/2;
if($w > $h)
{
$adjusted_width = $w/$hm;
$half_width = $adjusted_width / 2;
$int_width = $half_width - $w_height;
ImageCopyResampled($thumb,$img2,-$int_width,0,0,0,$adjusted_width,$nh,$w,$h);
if($ext=="jpg"):
ImageJPEG($thumb,$thname,95);
elseif($ext=="gif"):
ImageGIF($thumb,$thname,95);
elseif($ext=="png"):
@ImagePNG($thumb,$thname,95);
endif;
}
elseif(($w < $h) || ($w == $h))
{
$adjusted_height = $h / $wm;
$half_height = $adjusted_height / 2;
$int_height = $half_height - $h_height;
ImageCopyResampled($thumb,$img2,0,-$int_height,0,0,$nw,$adjusted_height,$w,$h);
if($ext=="jpg"):
ImageJPEG($thumb,$thname,95);
elseif($ext=="gif"):
ImageGIF($thumb,$thname,95);
elseif($ext=="png"):
ImagePNG($thumb,$thname,95);
endif;
}
else
{
ImageCopyResampled($thumb,$img2,0,0,0,0,$nw,$nh,$w,$h);
if($ext=="jpg"):
ImageJPEG($thumb,$thname,95);
elseif($ext=="gif"):
ImageGIF($thumb,$thname,95);
elseif($ext=="png"):
ImagePNG($thumb,$thname,95);
endif;
}
imagedestroy($img2);
$target=420;
$img=$tpath . "/" . $pid . "." . $ext;
$dimensions = GetImageSize($img);
$thname = $img;
$w=$dimensions[0];
$h=$dimensions[1];
$width=$w;
$height=$h;
if(($width>$target && $height>$target) || $width>$target):
$cent=100-round((($width-$target)/$width)*100);
$width=round($width*$cent/100);
$height=round($height*$cent/100);
elseif($height>$target):
$cent=100-round((($height-$target)/$height)*100);
$width=round($width*$cent/100);
$height=round($height*$cent/100);
endif;
if(($width>$target && $height>$target)|| $width>$target):
$cent=100-round((($width-$target)/$width)*100);
$width =round($width*$cent/100);
$height=round($height*$cent/100);
elseif($height>$target):
$cent=100-round((($height-$target)/$height)*100);
$width =round($width*$cent/100);
$height=round($height*$cent/100);
endif;
$nw=$width;$nh=$height;
if($ext=="jpg"):
$img2 = ImageCreateFromJpeg($img);
elseif($ext=="gif"):
$img2 = ImageCreateFromGif($img);
elseif($ext=="png"):
$img2 = ImageCreateFromPng($img);
endif;
$thumb=ImageCreateTrueColor($nw,$nh);
$wm = $w/$nw;
$hm = $h/$nh;
$h_height = $nh/2;
$w_height = $nw/2;
if($w > $h)
{
$adjusted_width = $w/$hm;
$half_width = $adjusted_width / 2;
$int_width = $half_width - $w_height;
ImageCopyResampled($thumb,$img2,-$int_width,0,0,0,$adjusted_width,$nh,$w,$h);
if($ext=="jpg"):
ImageJPEG($thumb,$thname,95);
elseif($ext=="gif"):
ImageGIF($thumb,$thname,95);
elseif($ext=="png"):
ImagePNG($thumb,$thname,95);
endif;
}
elseif(($w < $h) || ($w == $h))
{
$adjusted_height = $h / $wm;
$half_height = $adjusted_height / 2;
$int_height = $half_height - $h_height;
ImageCopyResampled($thumb,$img2,0,-$int_height,0,0,$nw,$adjusted_height,$w,$h);
if($ext=="jpg"):
ImageJPEG($thumb,$thname,95);
elseif($ext=="gif"):
ImageGIF($thumb,$thname,95);
elseif($ext=="png"):
ImagePNG($thumb,$thname,95);
endif;
}
else
{
ImageCopyResampled($thumb,$img2,0,0,0,0,$nw,$nh,$w,$h);
if($ext=="jpg"):
ImageJPEG($thumb,$thname,95);
elseif($ext=="gif"):
ImageGIF($thumb,$thname,95);
elseif($ext=="png"):
ImagePNG($thumb,$thname,95);
endif;
}
imagedestroy($img2);
endif;
endfor;
echo"<script>alert('Photo successfully Updated');window.location.href='property.pictures.php?id=" . $_REQUEST[id] . "';</script>";
endif;
$r=mysql_query("select * from " . $prev . "property where id=". $id);
$d=@mysql_fetch_array($r);
?>
Really Good Piece of Code esp. for image Resize, Thank you and God Bless U and I got another solution for image distortion problem, following function will help to create resize the image with good quality and thank to php.net……..
RegardsPHP Code:function ThumCreator($ImageSourcePath, $ImageDestinationPath, $InputWidth, $InputHeight, $ImageType = ""){
$ImageFormat = strtolower(substr(strrchr($ImageSourcePath,"."),1));
switch($ImageFormat){
case 'gif' :
$SourceType = "gif";
$NewBornImage = imagecreatefromgif($ImageSourcePath);
break;
case 'png' :
$SourceType = "png";
$NewBornImage = imagecreatefrompng($ImageSourcePath);
break;
case 'jpg' :
$SourceType = "jpg";
$NewBornImage = imagecreatefromjpeg($ImageSourcePath);
break;
case 'jpeg' :
$SourceType = "jpg";
$NewBornImage = imagecreatefromjpeg($ImageSourcePath);
break;
default :
die("ERROR; UNSUPPORTED IMAGE TYPE");
break;
}
list($SourceWidth,$SourceHeight) = getimagesize($ImageSourcePath);
$XOffset = 0;
$YOffset = 0;
if($ImageType == "thumb"){// To minimize destortion
if ($SourceWidth / $InputWidth > $SourceHeight/ $InputHeight){
$XTemp = $SourceWidth;
$XRatio = 1-((($SourceWidth/$SourceHeight)-($InputWidth/$InputHeight))/2);
$SourceWidth = $SourceWidth * $XRatio;
$XOffset = ($XTemp - $SourceWidth)/2;
}elseif($SourceHeight/ $InputHeight > $SourceWidth / $InputWidth){
$YTemp = $SourceHeight;
$YRatio = 1-((($InputWidth/$InputHeight)-($SourceWidth/$SourceHeight))/2);
$SourceHeight = $SourceHeight * $YRatio;
$YOffset = ($YTemp - $SourceHeight)/2;
}
}else{ //Added this else part -------------
$XTemp = $SourceWidth/$InputWidth;
$NewWidth = $InputWidth;
$NewHeight = $SourceHeight/$XTemp;
if ($NewHeight > $InputHeight){
$YTemp = $SourceHeight/$InputHeight;
$NewHeight = $InputHeight;
$NewWidth = $SourceWidth/$YTemp;
}
$InputWidth = round($NewWidth);
$InputHeight = round($NewHeight);
}
$DestinationImage = imagecreatetruecolor ($InputWidth, $InputHeight);
imagecopyresampled($DestinationImage, $NewBornImage, 0, 0, $XOffset, $YOffset, $InputWidth, $InputHeight, $SourceWidth, $SourceHeight);
if($SourceType=="gif"){
imagegif($DestinationImage,$ImageDestinationPath);
}elseif($SourceType=="jpg"){
imagejpeg($DestinationImage, $ImageDestinationPath);
}elseif($SourceType=="png"){
imagepng($DestinationImage, $ImageDestinationPath);
}elseif($SourceType=="bmp"){
imagewbmp($DestinationImage, $ImageDestinationPath);
}
return true;
}
sujith
http://sujithfem.byethost32.com
http://suzerain.isgreat.org/
http://oxygen.isgreat.org/
Last edited by sujithfem; May 30, 2008 at 01:26.
"The fear of the Lord is the beginning of wisdom"
Great code and works like it should.
I just wanted to say that I thought this was an awesome tutorial... I was getting ready to write my very own from scratch, but decided to have a look and see if someone else had already done it and make a tutorial... sure enough, you had and it was very well explained and laid out... Thank you.
Wow. This is a total hack job solution!


Script is amazingly simple, but once again you gotta way the benefits of it vs the long term affects.
It's super simple to implement, but creates way to much over head. Yeah there's not much over head here, but if you upload the image in correct size than there is no need for this. If you're saying this isn't possible than use an image resize script.
I usually use programm to resize my pictures,then upload the pictures to my host! Picture Resize Genius is a good choice, I usually batch resize my photos,it is fit for me;
Bookmarks