Page 1 of 1
GD Thumbnail Generation
Posted: Sun Jun 07, 2009 9:47 pm
by fangonk
I am trying to use the GD library to create thumbnails from uploaded images. The script is successfully uploading the images but no thumb is being created at upload. I have been looking at this script for a while and I can't see any obvious errors, so there must be something fundamentally wrong with my approach. Any ideas?
Code: Select all
$dor = $_GET['session_name'];
if (!empty($_FILES)) {
$tempFile = $_FILES['Filedata']['tmp_name'];
$targetPath = $_SERVER['DOCUMENT_ROOT'] . $_GET['folder'] . '/';
$targetPath = str_replace('//','/',$targetPath);
$targetFile = $targetPath . $_FILES['Filedata']['name'];
move_uploaded_file($tempFile,$targetFile);
}
if ( isSet($targetFile) )
{
define( 'THUMBNAIL_IMAGE_MAX_WIDTH', 150 );
define( 'THUMBNAIL_IMAGE_MAX_HEIGHT', 150 );
function generate_image_thumbnail( $targetFile, $targetPath )
{
list( $source_image_width, $source_image_height, $source_image_type ) = getimagesize( $targetFile );
switch ( $source_image_type )
{
case IMAGETYPE_GIF:
$source_gd_image = imagecreatefromgif( $targetFile );
break;
case IMAGETYPE_JPEG:
$source_gd_image = imagecreatefromjpeg( $targetFile );
break;
case IMAGETYPE_PNG:
$source_gd_image = imagecreatefrompng( $targetFile );
break;
}
if ( $source_gd_image === false )
{
return false;
}
$thumbnail_image_width = THUMBNAIL_IMAGE_MAX_WIDTH;
$thumbnail_image_height = THUMBNAIL_IMAGE_MAX_HEIGHT;
$source_aspect_ratio = $source_image_width / $source_image_height;
$thumbnail_aspect_ratio = $thumbnail_image_width / $thumbnail_image_height;
if ( $source_image_width <= $thumbnail_image_width && $source_image_height <= $thumbnail_image_height )
{
$thumbnail_image_width = $source_image_width;
$thumbnail_image_height = $source_image_height;
}
elseif ( $thumbnail_aspect_ratio > $source_aspect_ratio )
{
$thumbnail_image_width = ( int ) ( $thumbnail_image_height * $source_aspect_ratio );
}
else
{
$thumbnail_image_height = ( int ) ( $thumbnail_image_width / $source_aspect_ratio );
}
$thumbnail_gd_image = imagecreatetruecolor( $thumbnail_image_width, $thumbnail_image_height );
imagecopyresampled( $thumbnail_gd_image, $source_gd_image, 0, 0, 0, 0, $thumbnail_image_width, $thumbnail_image_height, $source_image_width, $source_image_height );
imagejpeg( $thumbnail_gd_image, $targetPath, 90 );
imagedestroy( $thumbnail_gd_image );
return true;
}
}
Re: GD Thumbnail Generation
Posted: Sun Jun 07, 2009 10:04 pm
by requinix
I don't see any call to generate_image_thumbnail...
Re: GD Thumbnail Generation
Posted: Sun Jun 07, 2009 10:13 pm
by fangonk
oopps I left the last bit off:
Code: Select all
$dor = $_GET['session_name'];
if (!empty($_FILES)) {
$tempFile = $_FILES['Filedata']['tmp_name'];
$targetPath = $_SERVER['DOCUMENT_ROOT'] . $_GET['folder'] . '/';
$targetPath = str_replace('//','/',$targetPath);
$targetFile = $targetPath . $_FILES['Filedata']['name'];
move_uploaded_file($tempFile,$targetFile);
}
if ( isSet($targetFile) )
{
define( 'THUMBNAIL_IMAGE_MAX_WIDTH', 150 );
define( 'THUMBNAIL_IMAGE_MAX_HEIGHT', 150 );
function generate_image_thumbnail( $targetFile, $targetPath )
{
list( $source_image_width, $source_image_height, $source_image_type ) = getimagesize( $targetFile );
switch ( $source_image_type )
{
case IMAGETYPE_GIF:
$source_gd_image = imagecreatefromgif( $targetFile );
break;
case IMAGETYPE_JPEG:
$source_gd_image = imagecreatefromjpeg( $targetFile );
break;
case IMAGETYPE_PNG:
$source_gd_image = imagecreatefrompng( $targetFile );
break;
}
if ( $source_gd_image === false )
{
return false;
}
$thumbnail_image_width = THUMBNAIL_IMAGE_MAX_WIDTH;
$thumbnail_image_height = THUMBNAIL_IMAGE_MAX_HEIGHT;
$source_aspect_ratio = $source_image_width / $source_image_height;
$thumbnail_aspect_ratio = $thumbnail_image_width / $thumbnail_image_height;
if ( $source_image_width <= $thumbnail_image_width && $source_image_height <= $thumbnail_image_height )
{
$thumbnail_image_width = $source_image_width;
$thumbnail_image_height = $source_image_height;
}
elseif ( $thumbnail_aspect_ratio > $source_aspect_ratio )
{
$thumbnail_image_width = ( int ) ( $thumbnail_image_height * $source_aspect_ratio );
}
else
{
$thumbnail_image_height = ( int ) ( $thumbnail_image_width / $source_aspect_ratio );
}
$thumbnail_gd_image = imagecreatetruecolor( $thumbnail_image_width, $thumbnail_image_height );
imagecopyresampled( $thumbnail_gd_image, $source_gd_image, 0, 0, 0, 0, $thumbnail_image_width, $thumbnail_image_height, $source_image_width, $source_image_height );
imagejpeg( $thumbnail_gd_image, $targetPath, 90 );
imagedestroy( $thumbnail_gd_image );
return true;
}
echo '1'; // Important so upload will work on OSX
}
$result = generate_image_thumbnail( $targetFile, $targetPath );
return $result
? array( $uploaded_image_path, $thumbnail_image_path )
: false;
}
$result = process_image_upload( 'Image1' );
Re: GD Thumbnail Generation
Posted: Sun Jun 07, 2009 10:18 pm
by fangonk
Its probably better if I just post the full script, sorry I was trying to sort out the unimportant stuff:
Code: Select all
<?php
error_reporting(E_ALL);
$dor = $_GET['session_name'];
if (!empty($_FILES)) {
$tempFile = $_FILES['Filedata']['tmp_name'];
$targetPath = $_SERVER['DOCUMENT_ROOT'] . $_GET['folder'] . '/';
$targetPath = str_replace('//','/',$targetPath);
$targetFile = $targetPath . $_FILES['Filedata']['name'];
move_uploaded_file($tempFile,$targetFile);
}
if ( isSet($targetFile) )
{
define( 'THUMBNAIL_IMAGE_MAX_WIDTH', 150 );
define( 'THUMBNAIL_IMAGE_MAX_HEIGHT', 150 );
function generate_image_thumbnail( $targetFile, $targetPath )
{
list( $source_image_width, $source_image_height, $source_image_type ) = getimagesize( $targetFile );
switch ( $source_image_type )
{
case IMAGETYPE_GIF:
$source_gd_image = imagecreatefromgif( $targetFile );
break;
case IMAGETYPE_JPEG:
$source_gd_image = imagecreatefromjpeg( $targetFile );
break;
case IMAGETYPE_PNG:
$source_gd_image = imagecreatefrompng( $targetFile );
break;
}
if ( $source_gd_image === false )
{
return false;
}
$thumbnail_image_width = THUMBNAIL_IMAGE_MAX_WIDTH;
$thumbnail_image_height = THUMBNAIL_IMAGE_MAX_HEIGHT;
$source_aspect_ratio = $source_image_width / $source_image_height;
$thumbnail_aspect_ratio = $thumbnail_image_width / $thumbnail_image_height;
if ( $source_image_width <= $thumbnail_image_width && $source_image_height <= $thumbnail_image_height )
{
$thumbnail_image_width = $source_image_width;
$thumbnail_image_height = $source_image_height;
}
elseif ( $thumbnail_aspect_ratio > $source_aspect_ratio )
{
$thumbnail_image_width = ( int ) ( $thumbnail_image_height * $source_aspect_ratio );
}
else
{
$thumbnail_image_height = ( int ) ( $thumbnail_image_width / $source_aspect_ratio );
}
$thumbnail_gd_image = imagecreatetruecolor( $thumbnail_image_width, $thumbnail_image_height );
imagecopyresampled( $thumbnail_gd_image, $source_gd_image, 0, 0, 0, 0, $thumbnail_image_width, $thumbnail_image_height, $source_image_width, $source_image_height );
imagejpeg( $thumbnail_gd_image, $targetPath, 90 );
imagedestroy( $thumbnail_gd_image );
return true;
}
echo '1'; // Important so upload will work on OSX
}
$result = generate_image_thumbnail( $targetFile, $targetPath );
return $result
? array( $uploaded_image_path, $thumbnail_image_path )
: false;
}
$simp = simplexml_load_file($dor);
$node = $simp->addChild('pic');
$node->addChild('image', 'files/'.$_FILES['Filedata']['name']);
$node->addChild('thumb', 'files/Thumbs/'.$_FILES['Filedata']['name']);
$s = simplexml_import_dom($simp);
$s->saveXML($dor);
echo $dor;
?>
Re: GD Thumbnail Generation
Posted: Sun Jun 07, 2009 11:06 pm
by McInfo
There is a problem with unmatched curly braces (}) between lines 74 and 87.
Edit: This post was recovered from search engine cache.
Re: GD Thumbnail Generation
Posted: Mon Jun 08, 2009 3:33 am
by fangonk
I just counted all of my brackets and I don't see an issue. Could you be more specific? I have made a few changes to the script since the last post so maybe I accidentally fixed the brackets.
Here is what I have now:
Code: Select all
<?php
error_reporting(E_ALL);
$dor = $_GET['session_name'];
if (!empty($_FILES)) {
$tempFile = $_FILES['Filedata']['tmp_name'];
$targetPath = $_SERVER['DOCUMENT_ROOT'] . $_GET['folder'] . '/';
$targetPath = str_replace('//','/',$targetPath);
$targetFile = $targetPath . $_FILES['Filedata']['name'];
move_uploaded_file($tempFile,$targetFile);
}
if ( isSet($targetFile) )
{
define( 'THUMBNAIL_IMAGE_MAX_WIDTH', 150 );
define( 'THUMBNAIL_IMAGE_MAX_HEIGHT', 150 );
function generate_image_thumbnail( $targetFile, $targetPath )
{
list( $source_image_width, $source_image_height, $source_image_type ) = getimagesize( $targetFile );
switch ( $source_image_type )
{
case IMAGETYPE_GIF:
$source_gd_image = imagecreatefromgif( $targetFile );
break;
case IMAGETYPE_JPEG:
$source_gd_image = imagecreatefromjpeg( $targetFile );
break;
case IMAGETYPE_PNG:
$source_gd_image = imagecreatefrompng( $targetFile );
break;
}
if ( $source_gd_image === false )
{
return false;
}
$thumbnail_image_width = THUMBNAIL_IMAGE_MAX_WIDTH;
$thumbnail_image_height = THUMBNAIL_IMAGE_MAX_HEIGHT;
$source_aspect_ratio = $source_image_width / $source_image_height;
$thumbnail_aspect_ratio = $thumbnail_image_width / $thumbnail_image_height;
if ( $source_image_width <= $thumbnail_image_width && $source_image_height <= $thumbnail_image_height )
{
$thumbnail_image_width = $source_image_width;
$thumbnail_image_height = $source_image_height;
}
elseif ( $thumbnail_aspect_ratio > $source_aspect_ratio )
{
$thumbnail_image_width = ( int ) ( $thumbnail_image_height * $source_aspect_ratio );
}
else
{
$thumbnail_image_height = ( int ) ( $thumbnail_image_width / $source_aspect_ratio );
}
$thumbnail_gd_image = imagecreatetruecolor( $thumbnail_image_width, $thumbnail_image_height );
imagecopyresampled( $thumbnail_gd_image, $source_gd_image, 0, 0, 0, 0, $thumbnail_image_width, $thumbnail_image_height, $source_image_width, $source_image_height );
imagejpeg( $thumbnail_gd_image, $targetPath, 90 );
imagedestroy( $thumbnail_gd_image );
return true;
}
echo '1'; // Important so upload will work on OSX
}
$result = generate_image_thumbnail( $targetFile, $targetPath );
$simp = simplexml_load_file($dor);
$node = $simp->addChild('pic');
$node->addChild('image', 'files/'.$_FILES['Filedata']['name']);
$node->addChild('thumb', 'files/Thumbs/'.$_FILES['Filedata']['name']);
$s = simplexml_import_dom($simp);
$s->saveXML($dor);
echo $dor;
?>
Re: GD Thumbnail Generation
Posted: Mon Jun 08, 2009 10:27 am
by Benjamin
Use
Code: Select all
tags when posting code in the forums.