resize and crop image on fly through server-sive scripting PHP

How to crop and resize the image submitted by the user, through the form using PHP, on the fly?
millisamiAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
iceboxmanConnect With a Mentor Commented:
Try the code below.
Put this into a file called something like thumb.php
To use:

thumb.php?img=mypicture.gif?w=150 to set picture to width of 150 and adjust height propotionally
thumb.php?img=mypicture.gif?h=150 to set picture to height of 150 and adjust width propotionally
thumb.php?img=mypicture.gif to set picture to the default height and adjust width propotionally

<?php
$defaultheight = 75;
$img = @$HTTP_GET_VARS["img"];
$TNheight = @$HTTP_GET_VARS["h"];
$TNwidth = @$HTTP_GET_VARS["w"];
if(!empty($img))
{
      $imagehw = getimagesize($img);
      $imagewidth = $imagehw[0];
      $imageheight = $imagehw[1];
}
else
      return;

if(!empty($TNheight)) {
      $t_height = $TNheight;
      $t_width = floor($imagewidth * ($t_height / $imageheight));   // Thumbnail width (Keep Proportional)
}
elseif(!empty($TNwidth)) {
      $t_width = $TNwidth;
      $t_height = floor($imageheight * ($t_width / $imagewidth));   // Thumbnail height (Keep Proportional)
}
else {
      $t_height = $defaultheight;  // Default Thumbnail height
      $t_width = floor($imagewidth * ($t_height / $imageheight));   // Thumbnail width (Keep Proportional)
}


// get and display jpeg images
if(stristr($img,".jpg") || stristr($img,".jpeg")){
     header("Content-type: image/jpeg");
     $srcimage = imagecreatefromjpeg($img);
     $height = imageSY($srcimage);
     $width = imageSX($srcimage);
     $destimage = imagecreatetruecolor($t_width,$t_height);
     imagecopyresampled ($destimage,$srcimage,0,0,0,0,$t_width,$t_height,$width,$height);
     ImageJPEG($destimage);
     ImageDestroy($destimage);
}
elseif(stristr($img,".gif")){
     header("Content-type: image/gif");
     $srcimage = imagecreatefromgif($img);
     $destimage = imagecreate(100,100);
     $width = imageSX($srcimage);
     $height = imageSY($srcimage);
     imagecopyresized ($destimage,$srcimage,0,0,0,0,$t_width,$t_height,$width,$height);
     ImageGIF($destimage);
     ImageDestroy($destimage);    
}
// on a problem geneterate an image with ERROR in it
else {
$im = imagecreatetruecolor(75, 125);
$blue = imagecolorallocate($im,0,0,200);
$red = imagecolorallocate($im,255,0,0);
imagestring($im,2,2,5,"ERROR",$red);
imageGIF($im);
imagedestroy($im);
}

?>
0
 
snoyes_jwCommented:
You should have the GD library installed.  Crop and resize in one step using imagecopyresized().

http://www.php.net/manual/en/ref.image.php
http://us3.php.net/manual/en/function.imagecopyresized.php
0
 
pjargonCommented:
You need imagemagick installed on the machine you're php'ing on.  Imagemagick is a powerful command line image editor.
You'll have to look through imagemagick manuals to figure out how it works..

Try running "convert", if it does something, that's imagemagick.

Umm..  to run from perl..
something like:
$somevariabletocatchoutput = shellexec("convert imagename -optionstoresizecropwhatever newimagename");
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
petoskey-001Commented:
The best way is just use the build in GD functions.  Specifically the ImageCopyResize function, or for better results the ImageCopyResampled.  It will take an existing image and resize it, placing it into a new image.  Resampled will look better then resized.

To resize your image,

1. upload the image file.
2. open the uploaded file, using ImageCreateFromJpeg or something similar
3. create a new image of the desired size using ImageCreate
4. using ImageCopyResize, or ImageCopyResampled to copy the uploaded image into the new size
5. Use ImagePNG, ImageJPEG or similar to output your image.

Checks these links for more information...

The PHP Manual on resize and resample functions
http://php.net/imagecopyresized
http://php.net/imagecopyresampled 

General Tutorial on using GD
http://nyphp.org/content/presentations/GDintro/

Specific Tutorial on using GD to resize images, with working demos
http://nyphp.org/content/presentations/GDintro/gd19.php
0
 
petoskey-001Commented:
Yeah, there you go.  iceboxman even added error checking.  You just have to wrap it with add your file upload script.
0
 
neesterCommented:
Here:

http://phpthumb.sourceforge.net/

BEST resizing watermarking etc... module on the NET!

if you need help you can contact the developer hes very helpful!
0
 
neesterCommented:
BTW the methods IceBoxMan said probably wont work with every type of image.

PHPTHumb DOES!

I have a working demo:

http://img.neester.com

I use it all the time!
its great!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.