Solved

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

Posted on 2004-08-27
8
9,401 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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…
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…

707 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now