fagnonk Posted June 8, 2009 Share Posted June 8, 2009 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? $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; } } Link to comment https://forums.phpfreaks.com/topic/161331-gd-thumbnail-generation/ Share on other sites More sharing options...
evanct Posted June 8, 2009 Share Posted June 8, 2009 Have you turned on notices? Link to comment https://forums.phpfreaks.com/topic/161331-gd-thumbnail-generation/#findComment-851345 Share on other sites More sharing options...
fagnonk Posted June 8, 2009 Author Share Posted June 8, 2009 Yeah, Im not getting any warnings. Here is what the full script looks like: <?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; ?> Link to comment https://forums.phpfreaks.com/topic/161331-gd-thumbnail-generation/#findComment-851346 Share on other sites More sharing options...
gevans Posted June 8, 2009 Share Posted June 8, 2009 if ( isSet($targetFile) ) check cassing if ( isset($targetFile) ) get rid of the following return $result ? array( $uploaded_image_path, $thumbnail_image_path ) : false; } Link to comment https://forums.phpfreaks.com/topic/161331-gd-thumbnail-generation/#findComment-851348 Share on other sites More sharing options...
fagnonk Posted June 8, 2009 Author Share Posted June 8, 2009 Hmm still no luck - I think there may be a problem with the $targetFile & $targetPath variables - but I have been looking at this too long so I am not sure what it could be. Here is the script with the corrected cassing: <?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; ?> Link to comment https://forums.phpfreaks.com/topic/161331-gd-thumbnail-generation/#findComment-851403 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.