Solved

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

Posted on 2004-08-27
8
9,414 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
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!

 
LVL 4

Accepted Solution

by:
iceboxman earned 125 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

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

615 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