Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2004-08-27
8
Medium Priority
?
9,418 Views
Last Modified: 2012-08-13
How to crop and resize the image submitted by the user, through the form using PHP, on the fly?
0
Comment
Question by:millisami
8 Comments
 
LVL 33

Expert Comment

by:snoyes_jw
ID: 11913642
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
 
LVL 1

Expert Comment

by:pjargon
ID: 11913656
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
 
LVL 7

Expert Comment

by:petoskey-001
ID: 11916937
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 4

Accepted Solution

by:
iceboxman earned 375 total points
ID: 11917659
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
 
LVL 7

Expert Comment

by:petoskey-001
ID: 11917699
Yeah, there you go.  iceboxman even added error checking.  You just have to wrap it with add your file upload script.
0
 
LVL 11

Expert Comment

by:neester
ID: 11924568
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
 
LVL 11

Expert Comment

by:neester
ID: 11924572
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question