J N
asked on
php create image on foreach loop
Hi,
i have the following code which works my uploading a photo to a directory. the script will then compare it against an thumbnail directory. if there is no thumbnail it creates one. it is all done by comparing the names. i cannot get it to work properly. the foreach loop works when there is no if statements within it. the if statements are done so it can support muliple formats.
how should i configure this to work
i have the following code which works my uploading a photo to a directory. the script will then compare it against an thumbnail directory. if there is no thumbnail it creates one. it is all done by comparing the names. i cannot get it to work properly. the foreach loop works when there is no if statements within it. the if statements are done so it can support muliple formats.
how should i configure this to work
<?php
// Turn off all error reporting
error_reporting(0);
//SCAN THE FOLDER AND CREATE THUMBNAILS
//BASED ON THE $DIRECTORY VALUE SET ON THE INDIVIDUAL PAGES
//ALLOW ONLY CERTAIN IMAGE TYPES
$files = glob("{$directory}*.{jpg,gif,png,jpeg}", GLOB_BRACE); //GIVES US THE ENTIRE LOCATION TO THE FILE
//REMOVE THE ENDING OF THE IMAGE TO MAKE A DESRIPTION
$file_types= array(".png",".jpeg",".jpg",".gif"); //match the allowable file types
//RECONFIGURE THE NAME TO REMOVE THE FULL PATH AND THE EXTENSION OF THE FILES
//REMOVE THE ENTIRE LENGTH OF THE FILE LOCATION AND GET JUST THE FILE NAMES
$short_names = str_replace($directory,"",$files);
//DETERMINE IF YOU WANT TO CREATE THUMBNAILS - SET ON INDIVIDUAL PAGES
if($thumbs ===TRUE){
//SET THE LOCATION OF THE THUMBNAILS BASED OFF OF THE DIRECTORY SET ON INDIVIDUAL PAGES
$thumb_directory= $directory."thumbnail/";
//CHECKS TO MAKE SURE THE THUMBDIRECTORY EXISTS IF NOT CREATE IT
if(is_dir($thumb_directory)!==TRUE){
mkdir($thumb_directory,0755);
}
//GET THE FILES FROM THUMBNAIL DIRECTORY
$tb_files = glob("{$thumb_directory}*.{jpg,gif,png,jpeg}", GLOB_BRACE); //GIVES US THE ENTIRE LOCATION TO THE FILE
//RECONFIGURE THE NAME TO REMOVE THE FULL PATH AND THE EXTENSION OF THE FILES
//REMOVE THE ENTIRE LENGTH OF THE FILE LOCATION AND GET JUST THE FILE NAMES
$tb_names = str_replace($thumb_directory,"",$tb_files);
//REMOVE THE THUMBNAIL PREFIX
$tb_prefix="tb_";
$tb_short_names =str_replace($tb_prefix,"",$tb_names);
//COMPARE THE THUMBS DIRECTORY TO CHECK IF IT HAS THE SAME INFORMATION--BASED ON THE SAME NAMES
$images_difference= array_diff($short_names, $tb_short_names);
//IF THERE ARE DIFFERENCES IN THE ARRAYS MAKE A THUMB
foreach($images_difference as $differences){
//RENAME THE THUMB
$rename_thumb = $tb_prefix.$differences;
//CREATE THE NEW IMAGE VARIABLE
$new_image =$directory.$differences;
//CONFIGURE THE THUMBNAILS
$new_height=$thumb_height;
$new_width=$thumb_width;
//GET ORIGNINAL IMAGE INFORMATION
list($width, $height) = getimagesize($new_image);
$thumb = imagecreatetruecolor($new_width, $new_height);
//FIGURE OUT THE EXTENSION OF THE FILE
$preg = pathinfo($differences, PATHINFO_EXTENSION);
//CREATE JPEG IMAGES
if($preg == "jpg" || $preg == "jpeg"){
$source = imagecreatefromjpeg($new_image);
//RESIZE THE IMAGE
imagecopyresized($thumb, $source, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
imagejpeg($thumb, $thumb_directory.$rename_thumb, 100);
echo "created:".$rename_thumb."PLEASE REFRESH"."<BR/>";
}elseif($preg == "png"){
//CREATE PNG IMAGES
$source = imagecreatefrompng($new_image);
//RESIZE THE IMAGE
imagecopyresized($thumb, $source, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
imagepng($thumb, $thumb_directory.$rename_thumb, 100);
echo "created:".$rename_thumb."PLEASE REFRESH"."<BR/>";
}elseif($preg == "gif"){
//CREATE GIF IMAGE
$source = imagecreatefromgif($new_image);
//RESIZE THE IMAGE
imagecopyresized($thumb, $source, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
imagegif($thumb, $thumb_directory.$rename_thumb, 100);
echo "created:".$rename_thumb."PLEASE REFRESH"."<BR/>";
}else{
//THE FILE IS NOT AN SUPPORTED IMAGE
echo "The file is not supported:".$new_image."<br/>";
}
}
}
?>
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
it was actually the php5.ini and the memory limit but both were excellent help
The memory limit may be an issue if your script creates many instances of an image resource without freeing the resource.
http://php.net/manual/en/function.imagedestroy.php
http://php.net/manual/en/function.imagedestroy.php
awsome - thanks for the points - not that I deserved them - did not see the param in pathinfo to return the extension which sort of makes my post ... wrong!
Thanks anyway
Thanks anyway
Refer to this link for info on how to use pathinfo
http://www.php.net/manual/en/function.pathinfo.php