?
Solved

Image resize / autofit

Posted on 2002-03-18
3
Medium Priority
?
343 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 100 total points
ID: 6878725
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
ID: 7135371
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
ID: 7219793
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

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
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 look for a specific file type in a local or remote server directory using PHP.
Suggested Courses
Course of the Month14 days, 11 hours left to enroll

839 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