News:

Pretty crazy that we're closer to 2030, than we are 2005. Where did the time go!

Main Menu

[PHP] Thumbnail Generation

Started by Sidoh, January 22, 2006, 04:32:56 PM

Previous topic - Next topic

0 Members and 3 Guests are viewing this topic.

Sidoh

This requires that GD is installed.  I will probably expand upon this later to include watermark/text inclusion on the thumbnails, but this is all you need for now.  This is a sector of code from a module I wrote for the CMS I'm working on, so it looks somewhat insignificant for a class alone:


<?php	class photos {		/**		 * Checks the mime type of the given filename.		 * // TODO: Make sure this works on windows servers		 *		 * @param string $f		 * @return string		 */		function check_file_type($f) {						// Escape the shell arguments			$f = escapeshellarg($f);						// Remove whitespace			$r = trim(`file -bi $f`);						/**			 * If there's a semi-colen, there's usually unwanted			 * data after it.  Remove it here:			 */			if( $p = strstr($r, ';') )				$r = substr($r, 0, ( strlen($r) - ( strlen($p) ) ) );			return $r;						}				/**		 * Generates a thumbnail from a file; if MODE_SAVE, the read directory		 * must be write accessable (mode 0777).		 *		 * @param int $x		 * @param int $y		 * @param string $filename		 * @param FORMAT $format		 * @param MODE $mode		 * @return bool		 */		function generate_thumbnail($resize_x, $resize_y, $filename, $thumbnail_format=FORMAT_PNG, $mode=MODE_SAVE) {						$f = $filename;			$c = $filename;						$e = $this->get_extension($f);						// Open the file as the correct format			switch( strtolower($e) ) {				case 'jpg':				case 'jpeg':									$im = imagecreatefromjpeg($f);								break;				case 'gif':					$im = imagecreatefromgif($f);					break;				case 'wbmp':					$im = imagecreatefromwbmp($f);					break;				case 'png':					$im = imagecreatefrompng($f);					break;			}						// Get the image resolution			$original_x = imagesx($im);			$original_y = imagesy($im);						// Resize according to the original size -- shorten 200 to maintain correct x:y ratio						// x:y ratio			$xyr = ( $original_x / $original_y );						// If x:y > 1, y is the smaller of x and y; reduce its thumbnail y			if( $xyr > 1 ) {								// Round... there aren't half pixels! 				$thumb_y = round( ( $resize_y / $xyr ), 0 );				$thumb_x = $resize_x;							} else if( $xyr < 1) { // If x:y < 1, x is the smaller of x and y; reduce its thumbnail x							$thumb_y = $resize_y;				$thumb_x = round( ( $resize_x * $xyr ), 0 );							} else if( $xyr == 1) { // In the rare event x:y = 1, don't change the thumbnail resolution							$thumb_y = $resize_y;				$thumb_x = $resize_x;							}						// Create an image object to write the thumbnail to			$thumb_im = imagecreatetruecolor($thumb_x, $thumb_y);						// Copy the original image to the thumbnail and resize it			imagecopyresized($thumb_im, $im, 0, 0, 0, 0, $thumb_x, $thumb_y, $original_x, $original_y);						// Destroy the original image object; we don't need it anymore			imagedestroy($im);						// Alright, we're done; process the image						if( $mode == MODE_SAVE ) {						// Generate a name for the thumbnail: <filename>-thumb.<extension>				$j = substr( $c, 0, ( strlen($c) - ( strlen($e) + 1 ) ) );								$thumb_url = "$j-thumb.$thumbnail_format";								// Write the thumbnail image to a file				eval("image$thumbnail_format(\$thumb_im, '$thumb_url');");								// If the file wasn't written, return false				if(! file_exists($thumb_url) )					return false;						} else if( $mode == MODE_OUTPUT ) {								// Display the image in the browser				header("Content-Type: image/$thumbnail_format");				eval("image$thumbnail_format(\$thumb_im);");							}							imagedestroy($thumb_im);						return true;					}				/**		 * Gets the extension of the given filename.		 *		 * @param string $f		 * @return string		 */		function get_extension($f) {						$e = explode('.', $f);						if( sizeof($e) ) {											$e = $e[ sizeof($e) - 1 ];								if( strstr($e, '/') ) {										return NULL;									} else {										return $e;									}								} else {								return NULL;							}					}	}?>

Ryan Marcus

Or be "uber" and use ImageMagick:

http://www.imagemagick.org/script/index.php

I think it is faster, but not as easy to code.
Thanks, Ryan Marcus

Quote
<OG-Trust> I BET YOU GOT A CAR!
<OG-Trust> A JAPANESE CAR!
Quote
deadly: Big blue fatass to the rescue!
496620796F75722072656164696E6720746869732C20796F75722061206E6572642E00

iago

I use ImageMagick to create my thumbnails from PHP scripts (most notably, OSPAP). 

However, I have one serious issue with doing that. 

ImageMagick is designed as a local program to work on your own files with your commands.  When you start calling it on other people's files through a PHP script, I worry that somebody will find a way to abuse it.  Since it's not designed to take in arbitrary input, I wouldn't be surprised if there are security vulnerabilities.  On the other hand, gd is designed to work on potentially malicious input, but I trust it with untrusted data more. 

Sidoh

Quote from: Ryan Marcus on June 19, 2006, 07:00:37 PM
Or be "uber" and use ImageMagick:

http://www.imagemagick.org/script/index.php

I think it is faster, but not as easy to code.

Present me with factual reason that I should use it in place of GD.

Joe

Because it's easier for iago the closet blackhat to exploit. :p
Quote from: Camel on June 09, 2009, 04:12:23 PMI'd personally do as Joe suggests

Quote from: AntiVirus on October 19, 2010, 02:36:52 PM
You might be right about that, Joe.


Sidoh

Quote from: Joe on June 20, 2006, 12:15:21 AM
Because it's easier for iago the closet blackhat to exploit. :p

I'll cut you.

iago

Quote from: Sidoh on June 19, 2006, 11:58:00 PM
Quote from: Ryan Marcus on June 19, 2006, 07:00:37 PM
Or be "uber" and use ImageMagick:

http://www.imagemagick.org/script/index.php

I think it is faster, but not as easy to code.

Present me with factual reason that I should use it in place of GD.

Well, it supports more formats.  That's a plus. 

Also, on OSPAP I kept running out of memory because of the size of the images (they're digital camera pictures, not small).  Rather than do the ini_set or whatever, using ImageMagick was easier. 

Plus, I like ImageMagick.  "convert -resize 800x600 image.png image-small-png"

Sidoh

Quote from: iago on June 20, 2006, 08:18:11 AM
Well, it supports more formats.  That's a plus. 

Also, on OSPAP I kept running out of memory because of the size of the images (they're digital camera pictures, not small).  Rather than do the ini_set or whatever, using ImageMagick was easier. 

Plus, I like ImageMagick.  "convert -resize 800x600 image.png image-small-png"

Didn't Ryan Marcus just say it was harder to code? :P

iago

#8
Quote from: Sidoh on June 20, 2006, 01:45:18 PM
Quote from: iago on June 20, 2006, 08:18:11 AM
Well, it supports more formats.  That's a plus. 

Also, on OSPAP I kept running out of memory because of the size of the images (they're digital camera pictures, not small).  Rather than do the ini_set or whatever, using ImageMagick was easier. 

Plus, I like ImageMagick.  "convert -resize 800x600 image.png image-small-png"

Didn't Ryan Marcus just say it was harder to code? :P

If he thinks that, then I disagree with him. 

<?php    function resize_image($width, $height, $compression, $old_filename, $new_filename)    {        $ret = 0;        system("convert -quality $compression -resize " . $width . "x" . $height . " $old_filename $new_filename", $ret);        if(!is_file($old_filename))        {            show_error_die('image failed to upload');        }        else if($ret == 127)        {            show_error_die('image conversion failed, please install imagemagick or edit the resize_image() function in configuration.php');        }        else if($ret != 0)        {            show_error_die('image conversion failed, picture was invalid or corrupt');        }    }?>



Doesn't look hard to me

Sidoh


deadly7

That's actually pretty neat.  I have use for something like this... :D I just wonder if I have GD built in. *runs phpinfo()*
[17:42:21.609] <Ergot> Kutsuju you're girlfrieds pussy must be a 403 error for you
[17:42:25.585] <Ergot> FORBIDDEN

on IRC playing T&T++
<iago> He is unarmed
<Hitmen> he has no arms?!

on AIM with a drunk mythix:
(00:50:05) Mythix: Deadly
(00:50:11) Mythix: I'm going to fuck that red dot out of your head.
(00:50:15) Mythix: with my nine

Sidoh

Quote from: deadly7 on June 23, 2006, 12:29:32 PM
That's actually pretty neat.  I have use for something like this... :D I just wonder if I have GD built in. *runs phpinfo()*

I think it'd be easier to tell with:

<?php  echo array_values( gd_info() );?>