PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
fangonk
Forum Newbie
Posts: 21 Joined: Thu May 14, 2009 6:43 am
Post
by fangonk » Sun Jun 07, 2009 9:47 pm
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;
}
}
Last edited by
Benjamin on Sun Jun 07, 2009 11:40 pm, edited 1 time in total.
Reason: Changed code type from text to php.
requinix
Spammer :|
Posts: 6617 Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA
Post
by requinix » Sun Jun 07, 2009 10:04 pm
I don't see any call to generate_image_thumbnail...
fangonk
Forum Newbie
Posts: 21 Joined: Thu May 14, 2009 6:43 am
Post
by fangonk » Sun Jun 07, 2009 10:13 pm
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' );
Last edited by
Benjamin on Sun Jun 07, 2009 11:41 pm, edited 1 time in total.
Reason: Changed code type from text to php.
fangonk
Forum Newbie
Posts: 21 Joined: Thu May 14, 2009 6:43 am
Post
by fangonk » Sun Jun 07, 2009 10:18 pm
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;
?>
Last edited by
Benjamin on Sun Jun 07, 2009 11:41 pm, edited 1 time in total.
Reason: Changed code type from text to php.
McInfo
DevNet Resident
Posts: 1532 Joined: Wed Apr 01, 2009 1:31 pm
Post
by McInfo » Sun Jun 07, 2009 11:06 pm
There is a problem with unmatched curly braces (}) between lines 74 and 87.
Edit: This post was recovered from search engine cache.
Last edited by
McInfo on Tue Jun 15, 2010 10:32 pm, edited 1 time in total.
fangonk
Forum Newbie
Posts: 21 Joined: Thu May 14, 2009 6:43 am
Post
by fangonk » Mon Jun 08, 2009 3:33 am
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;
?>
Last edited by
Benjamin on Mon Jun 08, 2009 10:26 am, edited 1 time in total.
Reason: Changed code type from text to php.
Benjamin
Site Administrator
Posts: 6935 Joined: Sun May 19, 2002 10:24 pm
Post
by Benjamin » Mon Jun 08, 2009 10:27 am
Use
Code: Select all
tags when posting code in the forums.