USE AT OWN RISK
Generated: Tue Jul 31 15:21:59 2007 from exif-test2.php 2005/11/01 5.1 KB bytes.
<? /** * This script will extract a thumbnail that may be imbedded into an image. If such * a thumbnail has not been found, a new thumbnail will be created. * It will ONLY function with JPG images ... Geoff. */ $meol = "\r\n"; echo "<html>$meol"; // One last point worth mention is that imagecreate() is dicey and you should use imagecreatetruecolor() instead. // Similarly it's better to use imagecopyresampled() instead of imagecopyresized(). So we have: // $dst_img=imagecreatetruecolor($new_imgSx,$new_imgSy); // ImageCopyResampled($dst_img,$src_img,0,0,0,0,$new_imgSx,$new_imgSy,$imgSx,$imgSy); // $file = $_FILES['userfile']; // $k = count($file['name']); $file = 'test.jpg'; $in_dir = '.'; // NOTE: must set register_argc_argv = On in php.ini if (isset($argc) && ($argc > 1)) { print "Got $argc command arguments ...<br>$meol"; if ( $argc > 2 ) { echo "ERROR: Can only give ONE file on command line ...<br>$meol"; exit(2); } for ( $i = 1; $i < $argc; $i++ ) { $arg = $argv[$i]; echo "Argument $i = $arg<br>$meol"; if ( file_exists( $arg ) ) { echo "Setting file to $arg ...<br>$meol"; $parts = split("/",$arg); $file = $parts[count($parts)-1]; // last part is FILE $part1 = array_slice($parts,0,count($parts)-1); // get all but last part $in_dir = implode("/", $part1); // and put path back together ... } else { echo "ERROR: FILE DOES NOT EXIST!<br>$meol"; exit(3); } } } $in_file = $in_dir . "/" . $file; if ( !file_exists( $in_file ) ) { print "ERROR: Failed to locate in file '$in_file'! Exiting ...<br>$meol"; exit(1); } $imageDir="images"; $file_size = filesize($in_file); echo "Loading file $in_file ... size = $file_size<br>$meol"; // $userfile_name = $file; $userfile_name = $in_file; // $parts = split("\.",$file['name'][$i]); $parts = split("\.",$file); $ext = $parts[count($parts)-1]; //$thumb_name = array_slice($parts,0,count($parts)-1); $thumb_name = array_slice($parts,0,count($parts)-1); $file_name = implode("", $thumb_name); //echo " got thumbname '". $thumb_name[0] . "' with extension $ext<br>$meol"; //echo " got thumbname '". implode("", $thumb_name) . "' with extension $ext<br>$meol"; echo " got thumbname '$file_name' with extension '$ext'<br>$meol"; $ext = strtolower($ext); if ($ext == "jpg") { // switch($ext) { case "jpg"; /* sometimes we may find that the image already contains an * embedded thumbnail. Then we simply extract that. */ // $thumb_data = exif_thumbnail("$imageDir/". $file['name'][$i]); // $thumb_data = exif_thumbnail($file); $thumb_data = exif_thumbnail($in_file); $thumb_name = join(".",$thumb_name) . "-t.jpg"; echo " Got thumbnail name = '$thumb_name' ...<br>$meol"; $out_name = $imageDir . "/" . $thumb_name; if($thumb_data) { if (file_exists( $out_name )) { $new_size = filesize($out_name); print "WARNING: $out_name ALREADY EXISTS ... Size = $new_size ...<br>$meol"; print "Delete, or rename, to run script ...<br>$meol"; } else { $fp = fopen("$out_name","wb"); fputs($fp,$thumb_data); $new_size = filesize($out_name); print " Embedded Thumbnail IMAGE written to disk, as '$out_name' ... size = $new_size<br>$meol"; } } else { if (file_exists( $out_name )) { $new_size = filesize($out_name); print "WARNING: $out_name ALREADY EXISTS ... Size = $new_size ...<br>$meol"; print "Delete, or rename, to run script ...<br>$meol"; } else { /* tough luck here comes work. */ //$src_img=ImageCreateFromJpeg("$imageDir/$userfile_name"); //$src_img=ImageCreateFromJpeg("$userfile_name"); print "No thumb data ... creating from file $userfile_name ...<br>$meol"; $src_img = imagecreatefromjpeg("$userfile_name"); print " Done ...<br>$meol"; if ($src_img) { print " got src_img ...<br>$meol"; } else { print " WARNING: got no src_img ...<br>$meol"; } /* get it's height and width */ $imgSx = imagesx($src_img); $imgSy = imagesy($src_img); if(($imgSx != 0) && ($imgSy != 0)) { print " src_img gave sizes $imgSx x $imgSy ...<br>$meol"; /* lets calculate the aspect ratio and the height * and width for the scaled image. */ $ratio = $imgSx/$imgSy; print " Ratio is $ratio ...<br>$meol"; if($ratio > 1) { $new_imgSx = 150; $new_imgSy = 150/$ratio; } else { $new_imgSx = (float) 150 * $ratio; $new_imgSy = 150; } print " New sizes $new_imgSx x $new_imgSy ...<br>$meol"; $dst_img = imagecreatetruecolor($new_imgSx,$new_imgSy); /* create the scaled instance */ ImageCopyResampled($dst_img,$src_img,0,0,0,0,$new_imgSx,$new_imgSy,$imgSx,$imgSy); /* write the damned thing to disk */ // imageJpeg($dst_img,"$imageDir/thumb/$thumb_name"); imageJpeg($dst_img,"$out_name"); $new_size = filesize($out_name); print " Thumbnail IMAGE written to disk, as '$out_name' ... size = $new_size<br>$meol"; } else { print " src_img failed to give sizes ...<br>$meol"; } } } } else { print "sorry hoss - ONLY jpg images processed ...<br>$meol"; } print "</p>$meol"; ?> </body> </html>