Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > PHP How-To
|
PHP How-To Post your "How do I do this with PHP?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 10th, 2006, 01:36 AM
Friend of Wrox
 
Join Date: Oct 2004
Posts: 564
Thanks: 0
Thanked 4 Times in 4 Posts
Default Image Resize after Upload

Hi everyone,

Here is the challenge I am facing. I have a page that uploads two graphic files to the server. Currently I can get the dimensions of the images, and warn the user is they exceed the size limits (350x350). I think that instead of a warning, I could just resize the images to the correct size, and the user will never know.

I am sure I am close, but would someone be able to direct as to the best way to do this?

I figure I can either resize after the upload process, or once the upload is complete and the images have been removed to the specific folders.

Thanks in advance,

Mike

Mike
EchoVue.com
__________________
Mike
EchoVue.com
 
Old July 12th, 2006, 06:19 AM
Friend of Wrox
 
Join Date: Jan 2006
Posts: 198
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I use the GD library to resample my digital camera pictures for thumbnails, medium view, and large view JPGs for hosting on my website. It takes a bit of work to get set up, but once you do, it works great. You can specify the JPEG quality level and even do graphic manipulation.

You can get the installation instructions and learn more here: http://www.php.net/manual/en/ref.image.php

Jon Emerson
http://www.jonemerson.net/
 
Old July 13th, 2006, 12:16 AM
Friend of Wrox
 
Join Date: Oct 2004
Posts: 564
Thanks: 0
Thanked 4 Times in 4 Posts
Default

I actually figured this one out. The trick was getting the image and then getting it saved back over itself once it was resaved.

I got this code from a comment posted on the PHP.net site, and tweaked it to get the desired functionality.

Code:
    function ResizeImage($file,$maxwdt,$maxhgt, $dest) {
       list($owdt,$ohgt,$otype)=@getimagesize($file);

      switch($otype) {
       case 1:  $newimg=imagecreatefromgif($file); break;
       case 2:  $newimg=imagecreatefromjpeg($file); break;
       case 3:  $newimg=imagecreatefrompng($file); break;
       default: echo "Unkown filetype (file $file, typ $otype)"; return;
      }

      if($newimg) {
       if($owdt>1500 || $ohgt>1200)
           list($owdt, $ohgt) = Resample($newimg, $owdt, $ohgt, 1024,768,0);

       Resample($newimg, $owdt, $ohgt, $maxwdt, $maxhgt);

           if(!$dest) return $newimg;

           if(!is_dir(dirname($dest)))
           mkdir(dirname($dest));

       switch($otype) {
         case 1: imagegif($newimg,dest); break;    
         case 2: imagejpeg($newimg,$dest,90); break; 
         case 3: imagepng($newimg,$dest);  break; 
       }

           imagedestroy($newimg);

       chmod($dest,0644);
      }
    }

    function Resample(&$img, $owdt, $ohgt, $maxwdt, $maxhgt, $quality=1) { 
      if(!$maxwdt) $divwdt=0;
       else $divwdt=Max(1,$owdt/$maxwdt);

      if(!$maxhgt) $divhgt=0;
       else $divhgt=Max(1,$ohgt/$maxhgt);

      if($divwdt>=$divhgt) {
       $newwdt=$maxwdt;
       $newhgt=round($ohgt/$divwdt);
      } else {
       $newhgt=$maxhgt;
       $newwdt=round($owdt/$divhgt);
      }

       $tn=imagecreatetruecolor($newwdt,$newhgt);
       if($quality)
           imagecopyresampled($tn,$img,0,0,0,0,$newwdt,$newhgt,$owdt,$ohgt);        
       else 
           imagecopyresized($tn,$img,0,0,0,0,$newwdt,$newhgt,$owdt,$ohgt);

       imagedestroy($img);

       $img = $tn;

       return array($newwdt, $newhgt);
    }
To use it, I just use the following line...

ResizeImage($imageName,350,350, $imageName);

Which would resize and resample so that the longest side is set at 350 pixels.


Mike
EchoVue.com





Similar Threads
Thread Thread Starter Forum Replies Last Post
resize image dhoward VB.NET 2002/2003 Basics 1 June 14th, 2011 04:05 AM
upload flash and resize .swf ahmedzakey1983 ASP.NET 2.0 Basics 3 December 5th, 2007 12:36 PM
Upload and Resize Images raaj Beginning PHP 2 June 30th, 2007 04:49 AM
Image Resize Magen Classic ASP Basics 5 January 23rd, 2007 12:17 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.