ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hi. I need to know how to resize an image in a PHP script. It only has to go smaller. Unfortunately, I can't seem to find anythinga bout it either in my PHP book or in the online documentation.
A function that does it for you would be great, but I'm prepared to code it myself, if somebody would just tell me where to start...
Im thinking "In the case of an image that's 3 times too big, read every third line of that image and put it into a new one. Then read every third column of that image, and put it into the final one."
So you see it doesn't have to be pretty (although pretty would be nice) - it just has to work.
Any ideas?
[edit]
I guess to make it pretty, I'd have to say "look at every block of nine pixels, take the average colour, and write that as a new pixel in the final image"... but I don't know how to do that either
Last edited by Napalm Llama; 04-05-2005 at 06:10 AM.
Looks like there actually is a function to do it in PHP, though, and it looks like this:
imagecopyresampled ( resource dst_image, resource src_image, int dst_x, int dst_y, int src_x, int src_y, int dst_w, int dst_h, int src_w, int src_h )
Well, looks like I'm going to have fun working that one out
Here's a thumbnailing function I wrote in PHP quite awhile ago. Should give you a good start, though. This particular one resizes it so that the width is always 100, and the height of the thumbnail is the same approximate ratio as the original.
PHP Code:
function MakeThumb($src, $dest) { $img = imagecreatefromjpeg($src);
// Check whether it really is an image if( !isimg($file) ) { die("bad image"); }
// Decide which image type we're working with switch( isimg($file) ) { case "jpg": $orig = imagecreatefromjpeg( $file ); header('Content-type: image/jpeg'); imagejpeg(resize($orig)); break;
// Does all the non image-type specific bits function resize( $imgin ) {
// Import globals global $xlim, $ylim, $hi_q;
// Get the current sizes $ox = imagesx( $imgin ); $oy = imagesy( $imgin );
// Get the percentage change from old to potential new $xdiff = $xlim / ( $ox/100 ); $ydiff = $ylim / ( $oy/100 );
// If new image is larger than old, just use old if( $xdiff > 100 && $ydiff > 100 ) { return $imgin; }
// Decide which dimension is the most oversized, and calculate ratio from that if( $xdiff > $ydiff ) { $ratio = $ydiff/100; } else { $ratio = $xdiff/100; }
// Calculate the new dimensions $nx = $ox * $ratio; $ny = $oy * $ratio;
// Make a new canvas for the output image $imgout = imagecreatetruecolor( $nx, $ny );
// Shrink old image and copy to new canvas if( $hi_q ) { imagecopyresampled($imgout, $imgin, 0, 0, 0, 0, $nx, $ny, $ox, $oy); } else { imagecopyresized($imgout, $imgin, 0, 0, 0, 0, $nx, $ny, $ox, $oy); }
// Send shrunken image back to the caller return $imgout;
}
?>
All you need in addition is a functions.php containing this:
<img src="img.php?file=bigpicture.jpg" alt="Thumbnail of BigPicture">
What do you think?
I'm working on a caching thingy at the moment, so it doesn't hog so much CPU time.
It's being used as part of the project described in my sig, which is why it has to refer out to the functions.php file.
At the moment all the application does as a whole is let you browse your file tree remotely, and execute shell commands as 'nobody'.
I'm going to add caching though, because if I'm browsing my pictures directory, all those thumbnails take quite a while to generate...
Last edited by Napalm Llama; 04-05-2005 at 11:02 AM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.