Hello again,
I am writing a thumbnail generation script, and am trying to do it one small part at a time.
It eventually will do the following things:
[ul][li]Read the original image stored in ~/media/img[/li]
[li]Extract image information such as the filename of the image, the directory the image resides in, the date the image was created (picture taken), and the dimensions of the image (height, width)[/li]
[li]Insert the filename, the date image was taken, and the (eventually) user the image belongs to a database table to be recalled by a CMS later.[/li]
[li]Generate a thumbnail from the original image and the information gathered[/li]
[li]Save the image to the director ~/media/thumbs_img[/li][/ul]
Currently I am having trouble getting the script to save the file into the directory I wish for it to save in. I wish to save the image that I have loaded (currently it’s the original image, I’ll work on that portion when I can get to it) to ‘~\media\thumbs_img’.
Here is my coded implementation so far:
[php]
<?php //This Script Will Generate Thumbnails From Uploaded Files (Or Existing Files in the img Directory) //It Will Also Attempt to Extract MetaInformation and Insert It Into A Database #CREATE THE FILE URLS $img_dir = 'C:\HADELWEB\Apache2.4\htdocs\DD\media\img\\'; $thumb_dir = 'C:\HADELWEB\Apache2.4\htodcs\DD\media\thumbs_img\\'; $file = 'acwork.jpg'; $path = $img_dir . $file; //FUNCTION LIST #DEFINE FILE SIZE CALCULATION FUNCTION function format_fsize($file_size, $precision) { #Format the File Size so it is always between 1 and 999.49 $base = log($file_size) / log(1024); #Create The Human Readble Unit Array $unit_suffix = array('BY', 'KB', 'MB'); #Assemble the Formatted File Size return round(pow(1024, $base - floor($base)), $precision) . ' ' . $unit_suffix[floor($base)]; } #IMAGE RESIZE THUMBNAIL CALCULATION #For Images where Height Exceeds Width function new_dim_height($h_orig, $w_orig, $constant_dim) { $h_raw = ($h_orig * $constant_dim)/$w_orig; $h_thumb = floor($h_raw); echo 'New Height: ' . $h_thumb . ''; return $h_thumb; } #For Images where Width Exceeds Height function new_dim_width($w_orig, $h_orig, $constant_dim) { $w_raw = ($w_orig * $constant_dim)/$h_orig; $w_thumb = floor($w_raw); echo 'New Width: ' . $w_thumb . '
'; return $w_thumb; } #For Images where Width Equals Height function new_dim_ratio($w_orig, $constant_dim) { $scaling_ratio = $w_orig / $constant_dim; echo 'Sizing Ratio: ' . $scaling_ratio . '
'; return $scaling_ratio; } #DATE REFORMATTING FUNCTION function prep_date($datetime) { list($date, $time) = explode(" ", $datetime); echo $date . ',' . $time . '
'; #Slashes Tell Where $SQL_date = preg_replace('/:/', '-', $date); echo $SQL_date . '
'; return $SQL_date; } #LOAD IMAGE function LoadJPEG($path, $file) { $jpg_img = imagecreatefromjpeg($path); #Check to see if image exists if($jpg_img) { } else { #Create A Black Image $jpg_img = imagecreatetruecolor(133, 100); $bgc = imagecolorallocate($jpg_img, 255, 255, 255); $tc = imagecolorallocate($jpg_img, 0, 0, 0); imagefilledrectangle($jpg_img, 0, 0, 133, 100, $bgc); #Show Error Message imagestring($jpg_img, 1, 6, 6, $file, $tc); } return $jpg_img; } #START IMAGE CHECK #Check to see if the file exists if(file_exists($path)) { #File Exists, Check to see if the file is a JPEG file if(exif_imagetype($path) == 2) { echo 'File type is valid JPEG file. Continuing Processing.
'; $_IMAGE = exif_read_data($path); #Change File Size Into Human Readble Form //Check if a number is present if(isset($_IMAGE['FileSize']) && !empty($_IMAGE['FileSize'])) { echo 'File size verified, commencing thumbnail generation.
'; #Reassign Variable from Array $file_size = $_IMAGE['FileSize']; #Call Formatting Function, assign to form size $form_size = format_fsize($file_size, 2); } else { echo 'NOTICE: File size missing! Omitting original file size; commencing thumbnail generation.
'; } #Obtain Dimensions of Target Image $_DIMS = getimagesize($path); if(isset($_DIMS) && !empty($_DIMS)) { #Assign Dimensions to Appropriate Variables $h_orig = $_DIMS['1']; $w_orig = $_DIMS['0']; $constant_dim = 100; echo 'Image Dimensions: ' . $w_orig . ', ' . $h_orig . '
'; #Determine Which Dimension is larger if($h_orig > $w_orig) { new_dim_height($h_orig, $w_orig, $constant_dim); } elseif($h_orig < $w_orig) { new_dim_width($w_orig, $h_orig, $constant_dim); } else { new_dim_ratio($w_orig, $constant_dim); } } else { die('Dimensions are missing from image! Thumbnail generation cannot continue! Back'); } #Prepare the Retrieved Date for database Insertion (sanitize first) $datetime = $_IMAGE['DateTimeOriginal']; prep_date($datetime); #Load the Image $img = LoadJPEG($path, $file); #Display Image imagejpeg($img, $thumb_dir . 'test_' . $file); imagedestroy($img); #Print The Final Image Properties [ DEBUG ] echo '
Image Properties
'; echo 'Filename: ' . $_IMAGE['FileName'] . '
'; echo '' . $form_size . '
'; echo '' . $_IMAGE['DateTimeOriginal'] . '
'; echo 'Height [Pels]: ' . $h_orig . '
'; echo 'Width [Pels]: ' . $w_orig . '
'; echo 'Image
'; echo '[/php]