exif-test4.php to HTML

index

USE AT OWN RISK

Generated: Tue Jul 31 15:21:59 2007 from exif-test4.php 2005/11/01 4.4 KB bytes.

<?php

/* AIM: Check if a file contains a thumbnail image
 If YES, extract and write to disk, AND
 try to COPY the jpeg, WITHOUT all the EXIF info, and
 write that to disk also ... check size of new file
 NOTE: Uses PHP built-in functions and does NOT require
 any tool kit!
 Geoff McLane - 1 November, 2005
*/

$timestart = microtime(); // get time array secs and usecs ...
$meol = "\r\n";
$in_file = "test/DSC00989.JPG";
//$in_file = "/homepage/russ8/images/page00/DSC00001.JPG";
if ( !file_exists( $in_file ) ) {
   echo "ERROR: Can NOT locate file '$in_file' ... aborting ...$meol";
   exit(1);
}
$file_size = filesize( $in_file );
// $in_file = 'path1/path2/path3/name.jpg.thumb.jpg';

// separate path and filename
$parts = split("/", $in_file);
$pc = count($parts);
if( $pc > 1 ) {
   $file_name = $parts[$pc-1]; // last part is FILE NAME
   $patha = array_slice($parts,0,$pc-1); // get array of parts, excluding last ...
   $file_path = implode( "/", $patha);
} else {
   $file_name = $parts[$pc-1]; // get FILE NAME
   $file_path = ".";
}
echo "Got path of '$file_path', and file name of '$file_name' ...size = $file_size bytes<br>$meol";

// separate file name and extention
$parts = split("\.", $file_name);
$pc = count($parts);
if( $pc > 1 ) {
   $file_ext = $parts[$pc-1]; // last part is FILE NAME
   $filea = array_slice($parts,0,$pc-1);
   $file_tit = implode( ".", $filea );
} else {
   $file_tit = $file_name;
   $file_ext = "";
}
echo "Got file title '$file_tit', extension '$file_ext' ... <br>$meol";
if (strtolower( $file_ext ) != 'jpg') {
   echo "ERROR: NOT a JPG file ... aborting ...<br>$meol";
   exit(2);
}

$src_img = imagecreatefromjpeg("$in_file");
// $src_img = LoadJpeg("$in_file");
if ( !$src_img ) {
   echo "ERROR: FAILED to load JPEG image ... aborting ...<br>$meol";
   exit(3);
}
// get it's height and width
$imgSx = imagesx($src_img);
$imgSy = imagesy($src_img);
if( ($imgSx == 0) || ($imgSy == 0) ) {
   echo "ERROR: FAILED to get image size! ... aborting ...<br>$meol";
   exit(4);
}
echo "Loaded '$in_file', of size $imgSx X $imgSy ...<br>$meol";
$thumb_name = $file_tit . '-t.' . $file_ext;
// see if there is ALREADY a thumbnail image embedded ...
$thumb_data = exif_thumbnail($in_file, $in_width, $in_height, $in_type);
if ( $thumb_data ) {
   echo "Got thumbnail image $in_width X $in_height ($in_type) ...<br>$meol";
   $fp = fopen("$thumb_name","wb");
   fputs($fp,$thumb_data);
   fclose($fp);
   $th_size = filesize($thumb_name);
   print " Embedded Thumbnail IMAGE written to disk, as '$thumb_name' ... size = $th_size<br>$meol";
   $new_jpeg = imagecreatetruecolor($imgSx, $imgSy); /* Create a black image */
   if ( !$new_jpeg ) {
   echo "WARNING: FAILED to create new image ... <br>$meol";
   } else {
   if( imagecopy($new_jpeg,$src_img,0,0,0,0,$imgSx,$imgSy) ) {
   $new_name = $file_tit . '-n.' . $file_ext;
   if( imagejpeg( $new_jpeg, $new_name, 75 ) ) {
   $new_size = filesize($new_name);
   echo "New image written to $new_name ... New size = $new_size bytes ...<br>$meol";
   } else {
   echo "WARNING: Write New image to $new_name FAILED!...<br>$meol";
   }
   } else {
   echo "WARNING: FAILED to COPY image ...<br>$emeol";
   }
   imagedestroy( $new_jpeg );
   }
} else {
   echo "WARNING: No thumbnail image embedded in this JPEG file ...<br>$meol";
}

$timeend = microtime();
$time = $timeend - $timestart;
echo "Script ran for $time seconds ...<br>$meol";

// unused function
//function LoadJpeg($imgname) 
//{
// $im = @imagecreatefromjpeg($imgname); /* Attempt to open */
// if (!$im) { /* See if it failed */
// $im = imagecreatetruecolor(150, 30); /* Create a black image */
// $bgc = imagecolorallocate($im, 255, 255, 255);
// $tc = imagecolorallocate($im, 0, 0, 0);
// imagefilledrectangle($im, 0, 0, 150, 30, $bgc);
// /* Output an errmsg */
// imagestring($im, 1, 5, 5, "Error loading $imgname", $tc);
// }
// return $im;
//}

// unused - output directly to BROWSER
// note: this does NOT appear to work in a local
// command line use ...
//function OutIData($newimage) {
//ob_start(); // start a new output buffer
// imagejpeg( $newimage, "", 75 );
// $ImageData = ob_get_contents();
// $ImageDataLength = ob_get_length();
//ob_end_clean(); // stop this output buffer
//header("Content-type: image/jpeg") ;
//header("Content-Length: ".$ImageDataLength);
//echo $ImageData;
// }

?>

index

Valid HTML 4.01 Transitional