Solved

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

Posted on 2004-08-27
8
9,410 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

831 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