Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > PHP/MySQL > PHP How-To
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old July 10th, 2006, 01:36 AM
Friend of Wrox
 
Join Date: Oct 2004
Location: Clinton, UT, USA.
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
Reply With Quote
  #2 (permalink)  
Old July 12th, 2006, 06:19 AM
Friend of Wrox
 
Join Date: Jan 2006
Location: San Francisco, CA, USA.
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/
Reply With Quote
  #3 (permalink)  
Old July 13th, 2006, 12:16 AM
Friend of Wrox
 
Join Date: Oct 2004
Location: Clinton, UT, USA.
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
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 02:12 PM.


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