Solved

Image resize / autofit

Posted on 2002-03-18
3
325 Views
Last Modified: 2012-06-27
Hi, thanks to the help of Experts, I have managed to make a util to allow users on my site to browse to an image on their disks and upload it to my server and automatically display it on their pages,
Now, I want to go one step further, to automatically resize / autofit to the best dimensions in the space that I provide.

I have seen comments about imagemagic, but couldn't find examp0les simple enough for me to understand. Could someone here pleaae try to give me an example of how to do this.
It doesn't HAVE to be with using imagemagic, a simple algorith reading the width and height of image at upload time adn then proportionally resizing the image to fit within a pre-set (x,y) will do also.

Thanks
0
Comment
Question by:psimation
  • 2
3 Comments
 

Accepted Solution

by:
icewolf_li earned 50 total points
Comment Utility
At first you should install GD library for php, then u can use gd functions as follow:

/*
rend a new $dstFile from $srcFile,with the max size limit $maxsize.
*/
function RendSmall($srcFile,$dstFile,$maxsize) {
     
     $data = GetImageSize($srcFile,&$info);
     switch ($data[2]) {
          case 1:
          $im = @ImageCreateFromGIF($srcFile);
          break;
          case 2:
          $im = @imagecreatefromjpeg($srcFile);
          break;
          case 3:
          $im = @ImageCreateFromPNG($srcFile);
          break;
          default:
               return -1;
     }
     if(!$im)
          return -1;

     $dstW = $data[0];
     $dstH = $data[1];
     $dstX = 0;
     $dstY = 0;
     if($data[0]>=$data[1]){
          if($data[0] > $maxsize){
               $dstW = $maxsize;
               $dstH = $maxsize * $data[1] / $data[0];
               $dstY = ($maxsize-$dstH)/2;
          }
          else{
               $maxsize = $data[0];
               $dstY = ($data[0]-$dstH)/2;
          }
     }
     else{
          if($data[1] > $maxsize){
               $dstW = $maxsize * $data[0] / $data[1];
               $dstH = $maxsize;
               $dstX = ($maxsize-$dstW)/2;

          }
          else{
               $maxsize = $data[1];
               $dstX = ($data[1]-$dstH)/2;
          }
     }
     $srcW=ImageSX($im);
     $srcH=ImageSY($im);
     $ni=ImageCreate($maxsize,$maxsize);
     
     if(!$ni || !(ImageCopyResized($ni,$im,$dstX,$dstY,0,0,$dstW,$dstH,$srcW,$srcH)) )
          return -1;
     if(!ImageJpeg($ni,$dstFile))
          return -1;
     
     return 1;
}
0
 
LVL 17

Author Comment

by:psimation
Comment Utility
Sorry for the delay.
Can you help me to get GD installed first? I tried, but cannot get it right.
HAve RH7.0 with PHP that seems to have been installed with the --without-gd switch, I don't want to re-istall the entire PHP, so if you have  a solution to load it at runtime, it would be much better solution.

Thanks
0
 
LVL 17

Author Comment

by:psimation
Comment Utility
OK, this question didn't help me as I couldn't get GD installed, but I guess, for others out there, it might help, so there are merrit in having this question in the archives...
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

763 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

6 Experts available now in Live!

Get 1:1 Help Now