Re: RE: RE: Coding Help

From: Date: Wed, 18 Oct 2000 18:34:19 +0000
Subject: Re: RE: RE: Coding Help
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-20957@lists.php.net to get a copy of this message
I'd do it using external tools. For example: /* * The thumbnail function uses the pnmscale program from * the popular Netpbm image manipulation tools package * to create a thumbnail of the given image. If a thumbnail * already exists for the image, the function simply returns. */ function thumbnail($filename) { global $theight, $djpeg, $cjpeg, $pnmscale, $giftopnm, $ppmtogif, $ppmquant, $pnmpad, $shadow; $tdir = dirname($filename)."/thumbnails"; /* thumbnail directory */ if(!filetype($tdir)) { if(!@mkdir($tdir,0777)) { echo "Unable to create $tdir directory - check permissions<br>\n"; return; } } $tfile = $tdir."/".basename($filename); /* thumbnail file */ $tfile = ereg_replace("\.[^\.]+$",".gif",$tfile); if(!@filesize($tfile)) { if(eregi("\.gif$",$filename)) { /* Look for .gif extension */ exec("$giftopnm $filename | $pnmscale -height $theight | $pnmpad -white -l6 -b6 | $ppmquant 248 | $ppmtogif | $shadow > $tfile"); } elseif(eregi("\.jpe?g",$filename)) { /* Look for .jpg or .jpeg extension */ exec("$djpeg $filename | $pnmscale -height $theight | $pnmpad -white -l6 -b6 | $ppmquant 248 | $ppmtogif | $shadow > $tfile"); } else { /* not a GIF or JPG file */ return(""); } } return($tfile); } The various $djpeg, $cjpeg, etc. are just set to the actual locations of the corresponding programs on your system. And $theight specifies the height of the thumbnail in pixels. The aspect ratio is kept so the width will vary. You can fix both the height and the width, but with arbitrarily sized images coming in you will get some weird aspect changes happening. -Rasmus On Wed, 18 Oct 2000, Gavster wrote: > Hi Marc - thanks for the reply. > > Yeah, I know about fake resize - but what I'm talking about is giving users > the ability to upload a photo (it's for a personals site), and then not > only making a thumbnail copy, (I could even use some of the code I've found > in the ever so handy "Professional PHP Programming" tome, but also resizing > the original photo. Given that these photos will be taking up disk space, > I'd like to set maximums on the dimensions, and if the photo is over these, > to resize it. I know that I can limit the file size, etc. - and the way I'm > currently doing it is checking the uploaded image dimensions and bouncing > to an error page if they are out of range. > > I guess what I'm really after is a mathematical (probably geometrical, > actually) algorithm that is currently escaping me. > To wit: Give an photo of n x n, how to resize to say, 300 pixels high (or > under) while maintaining the aspect ratio of the original photo. This same > algorithm would work with both the full size and thumbnail photo. > > In order to do this with the Image functions in PHP, my understanding is > that you need to use a function like imageCreateFromJPG() or whatever. But > because no two uploaded photos will be the same size, I just need to get > output <= my max dimensions . . . . > > It's just figuring out how to size the output that's got me stumped . . . I > guess there were too many bong hits before Algebra Class lo those many > years ago (or maybe not enough?). . . . . . . . LOL > > > > At 05:52 AM 10/18/00 -0400, you wrote: > Does anyone have a generalize algorithm for making thumbnails out of a > photo of unkown size? > That is, I'm not going to know the dimensions of the source photo > beforehand, and what I'd like to do is to reduce the photo to about 60-80 > pixels high, depending on the aspect ratio . . . . > Any tips would be greatly appreciated. =)) > > > (1) If you mean a 'fake' resize (simply giving the image tag fake width and > height measurements and letting the browser crush it down): > Note the getimagesize() function. Among other things, it returns to you the > width and height of the image. > You can simply pick a fixed width and fixed height you'd like to work with. > Say, 80 and 80. > Read both the width and height and check which is higher. Then divide the > other one by the longer and multiply it by 80. > Then, simply spit out the image tag with 80 in the longer of width and > height, and the integer returned in the shorter one. > (2) If you mean a 'real' resize (actually saving another copy of the image, > in order to save the user download time when looking at thumbnails): > In PHP, I don't know. We generally create all our images on Macs, and so we > automate this process in Photoshop using PhotoScripter and AppleScripts. > Then the files are copied to the Web server over AppleTalk. > You would be able to accomplish it in PHP, perhaps with the GD library or > ImageMagick, though. > Anyone else? > > Cheers, > -- > Marc Antony Vose > http://www.suzerain.com/ > Into every tidy scheme for arranging the pattern of human life, it is > necessary to inject a certain dose of anarchism. > -- Bertrand Russell > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net > For additional commands, e-mail: php-general-help@lists.php.net > To contact the list administrators, e-mail: php-list-admin@lists.php.net > > > >

« previous php.general (#20957) next »