Solved

Image resize / autofit

Posted on 2002-03-18
3
333 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
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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Suggested Solutions

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
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…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

809 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