Author Topic: [PHP] Thumbnail Generation  (Read 6265 times)

0 Members and 1 Guest are viewing this topic.

Offline Sidoh

  • Moderator
  • Hero Member
  • *****
  • Posts: 17634
  • MHNATY ~~~~~
    • View Profile
    • sidoh
[PHP] Thumbnail Generation
« on: January 22, 2006, 04:32:56 pm »
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:

Code: [Select]
<?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($r0, ( 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 ) {

// Round... there aren't half pixels! 
$thumb_y round( ( $resize_y $xyr ), );
$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 ), );

} 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$im0000$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$c0, ( strlen($c) - ( strlen($e) + ) ) );

$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 $esizeof($e) - ];

if( strstr($e'/') ) {

return NULL;

} else {

return $e;

}

} else {

return NULL;

}

}
}

?>

Offline Ryan Marcus

  • Cross Platform.
  • Full Member
  • ***
  • Posts: 170
  • I'm Bono.
    • View Profile
    • My Blog
Re: [PHP] Thumbnail Generation
« Reply #1 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.
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

Offline iago

  • Leader
  • Administrator
  • Hero Member
  • *****
  • Posts: 17914
  • Fnord.
    • View Profile
    • SkullSecurity
Re: [PHP] Thumbnail Generation
« Reply #2 on: June 19, 2006, 07:27:02 pm »
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. 

Offline Sidoh

  • Moderator
  • Hero Member
  • *****
  • Posts: 17634
  • MHNATY ~~~~~
    • View Profile
    • sidoh
Re: [PHP] Thumbnail Generation
« Reply #3 on: June 19, 2006, 11:58:00 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.

Offline Joe

  • B&
  • x86
  • Hero Member
  • *****
  • Posts: 10319
  • In Soviet Russia, text read you!
    • View Profile
    • Github
Re: [PHP] Thumbnail Generation
« Reply #4 on: June 20, 2006, 12:15:21 am »
Because it's easier for iago the closet blackhat to exploit. :p
I'd personally do as Joe suggests

You might be right about that, Joe.


Offline Sidoh

  • Moderator
  • Hero Member
  • *****
  • Posts: 17634
  • MHNATY ~~~~~
    • View Profile
    • sidoh
Re: [PHP] Thumbnail Generation
« Reply #5 on: June 20, 2006, 01:01:33 am »
Because it's easier for iago the closet blackhat to exploit. :p

I'll cut you.

Offline iago

  • Leader
  • Administrator
  • Hero Member
  • *****
  • Posts: 17914
  • Fnord.
    • View Profile
    • SkullSecurity
Re: [PHP] Thumbnail Generation
« Reply #6 on: June 20, 2006, 08:18:11 am »
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"

Offline Sidoh

  • Moderator
  • Hero Member
  • *****
  • Posts: 17634
  • MHNATY ~~~~~
    • View Profile
    • sidoh
Re: [PHP] Thumbnail Generation
« Reply #7 on: June 20, 2006, 01:45:18 pm »
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

Offline iago

  • Leader
  • Administrator
  • Hero Member
  • *****
  • Posts: 17914
  • Fnord.
    • View Profile
    • SkullSecurity
Re: [PHP] Thumbnail Generation
« Reply #8 on: June 20, 2006, 01:48:12 pm »
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. 

Code: [Select]
<?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
« Last Edit: June 23, 2006, 05:16:11 pm by iago »

Offline Sidoh

  • Moderator
  • Hero Member
  • *****
  • Posts: 17634
  • MHNATY ~~~~~
    • View Profile
    • sidoh
Re: [PHP] Thumbnail Generation
« Reply #9 on: June 20, 2006, 01:56:59 pm »
I think it is faster, but not as easy to code.

 :-\

Offline deadly7

  • 42
  • Moderator
  • Hero Member
  • *****
  • Posts: 6496
    • View Profile
Re: [PHP] Thumbnail Generation
« Reply #10 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()*
[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

Offline Sidoh

  • Moderator
  • Hero Member
  • *****
  • Posts: 17634
  • MHNATY ~~~~~
    • View Profile
    • sidoh
Re: [PHP] Thumbnail Generation
« Reply #11 on: June 23, 2006, 05:10:26 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:

Code: [Select]
<?php
  
echo array_valuesgd_info() );
?>