dittmerdan Posted August 25, 2009 Share Posted August 25, 2009 <?php // Random 6 didgit Numbers for filename $randnumb1 = rand(000000,999999); $randnumb2 = rand(000000,999999); $randnumb3 = rand(000000,999999); $randnumb4 = rand(000000,999999); $randnumb5 = rand(000000,999999); $randnumb6 = rand(000000,999999); // Path to store the photos at $path1= "upload/$randnumb1-".$HTTP_POST_FILES['ufile']['name'][0]; $path2= "upload/$randnumb2-".$HTTP_POST_FILES['ufile']['name'][1]; $path3= "upload/$randnumb3-".$HTTP_POST_FILES['ufile']['name'][2]; $path4= "upload/$randnumb4-".$HTTP_POST_FILES['ufile']['name'][3]; $path5= "upload/$randnumb5-".$HTTP_POST_FILES['ufile']['name'][4]; $path6= "upload/$randnumb6-".$HTTP_POST_FILES['ufile']['name'][5]; // Uploading files to the path copy($HTTP_POST_FILES['ufile']['tmp_name'][0], $path1); copy($HTTP_POST_FILES['ufile']['tmp_name'][1], $path2); copy($HTTP_POST_FILES['ufile']['tmp_name'][2], $path3); copy($HTTP_POST_FILES['ufile']['tmp_name'][3], $path4); copy($HTTP_POST_FILES['ufile']['tmp_name'][4], $path5); copy($HTTP_POST_FILES['ufile']['tmp_name'][5], $path6); // Photo Preview echo "<img src=\"$path1\" width=\"150\" height=\"150\">"; echo "<img src=\"$path2\" width=\"150\" height=\"150\">"; echo "<img src=\"$path3\" width=\"150\" height=\"150\">"; echo "<br/>"; echo "<img src=\"$path4\" width=\"150\" height=\"150\">"; echo "<img src=\"$path5\" width=\"150\" height=\"150\">"; echo "<img src=\"$path6\" width=\"150\" height=\"150\">"; $filesize1 = $HTTP_POST_FILES['ufile']['size'][0]; $filesize2 = $HTTP_POST_FILES['ufile']['size'][1]; $filesize3 = $HTTP_POST_FILES['ufile']['size'][2]; $filesize4 = $HTTP_POST_FILES['ufile']['size'][3]; $filesize5 = $HTTP_POST_FILES['ufile']['size'][4]; $filesize6 = $HTTP_POST_FILES['ufile']['size'][5]; ?> I want the script to resize images to file at 800x600 so it lowers disk usage space. and I would like to rename all images coming in at equal lenth names. so to drop the filename like myimage.jpg and i want to names to be 564334.jpg using the rand function. or better yet have it count up. first picture 000001.jpg then second is 000002 to keep track of all the photos uploaded please help thanks Quote Link to comment Share on other sites More sharing options...
bluebyyou Posted August 25, 2009 Share Posted August 25, 2009 for the resizing portion of your problem you can start with imagecopyresized(). the manual has good documentation on how to use it. Quote Link to comment Share on other sites More sharing options...
Alex Posted August 25, 2009 Share Posted August 25, 2009 I wrote a quick class for image manipulation. It's quick, so it's not the best but it'll do everything you want. I also wrote pretty good documentation for it and wrote some examples. It'll help you with the resizing at least.. Class: <?php /* @ImageClass v0.0.4 Started: 8/10/09 Last Edit: 8/24/09 By:>>Alexander Allen @Vars>> $image $image>> Image Resource $ext $ext>> Image Resource Extension $file $file>> Filename of the original image @Methods>> __construct([$im]) << Creates a new Image class instance $im>> (Optional) filename setImage($im) << Sets, or replaces the current image instance $im>> filename resize($width, $height[, $porportional=FALSE[, $percent=NULL[, $max=NULL]]]) << Resizes the current image instance $width>> New width for the image $height>> New height for the image $porportional>> (Optional) If set the image is scaled down portportionally $percent>> (Optional) This is required if $porportional is set to true. It determines the size of the new image in % (0-100) $max>> (Optional) This sets the max width or height allowed. If either is over the limit the image will be scaled down porportionally. crop($width, $height, $x, $y) << Crops the current image instance $width>> New width for the image $height>> New height for the image $x>> Determines the x start-point $y>> Determines the y start-point $position>> (Optional) When set it overrides the $x and $y positions given and crops to the set position. Valid values are: >> CENTER, CENTER_LEFT, CENTER_RIGHT, TOP_CENTER, TOP_LEFT, TOP_RIGHT, BOTTOM_CENTER, BOTTOM_LEFT, BOTTOM_RIGHT save($loc[, $compression=0]) << Saves the current image instance $loc>> The path and file name to save the image to $compression>> (Optional) Determines the compression level (Default: 0) output() << Outputs the current image instance to the browser No Parameters Additional Notes>> 8/12/09>> - Added optional position parameter to cropping. - Added output() - Changed $im parameters for __construct() and setimage(), it now takes a filename instead of a image resource - Added var $file, contains the filename of the original Image Resource - Added Example 2 8/23/09>> - Modified resize() to support setting a max size (X|Y) 8/24/09>> - Some small clean-ups / optimizations - Better Error reporting @Examples>> Example 1>> $image = new Image('test.png'); $image->resize(null, null, true, 50); $image->save('images/newimage.png'); >> This example will first create an image from the resource 'test.png'. It then resizes it porportionally to a 50% scale. Finally the image is saved to images/newimage.png. Example 2>> <?php include('image.class.php'); $image = new Image('test.gif'); $image->crop(50, 50, null, null, CENTER_LEFT); $image->output(); ?> >> This example first creates an image resource from test.gif It then crops it to a 50, 50 square using the CENTER_LEFT positioning Finally the image is output and displayed in the browser. */ class Image { var $image, $ext, $file; public function __construct($im=NULL) { define('CENTER', 0, true); define('CENTER_LEFT', 1, true); define('CENTER_RIGHT', 2, true); define('TOP_CENTER', 3, true); define('TOP_LEFT', 4, true); define('TOP_RIGHT', 5, true); define('BOTTOM_CENTER', 6, true); define('BOTTOM_LEFT', 7, true); define('BOTTOM_RIGHT', 8, true); if(empty($im)) return true; $info = @getimagesize($im); $backtrace = debug_backtrace(); if(!$info) return die('<b>Fatal error:</b> \'' . $im . '\' is not a valid image resource on <b>line ' . $backtrace[0]['line'] . ' [' . $backtrace[0]['file'] . ']</b>'); $ex = explode('/', $info['mime']); $this->ext = $ex[1]; $func = 'imagecreatefrom' . $this->ext; $this->image = @$func($im); $this->file = $im; return true; } public function setImage($im) { $info = @getimagesize($this->file); $backtrace = debug_backtrace(); if(!$info) return die('<b>Fatal error:</b> \'' . $im . '\' is not a valid image resource on <b>line ' . $backtrace[0]['line'] . ' [' . $backtrace[0]['file'] . ']</b>'); $ex = explode('/', $info['mime']); $this->ext = $ex[1]; $func = 'imagecreatefrom' . $this->ext; $this->image = @$func($im); $this->file = $im; return true; } public function resize($width, $height, $porportional=FALSE, $percent=NULL, $max=NULL) { $backtrace = debug_backtrace(); if(empty($this->image)) return die('<b>Fatal error:</b> Invalid call to Image()->resize, no image is set on <b>line ' . $backtrace[0]['line'] . ' [' . $backtrace[0]['file'] . ']</b>'); $info = Array(imagesx($this->image), imagesy($this->image)); if($porportional) { if(empty($max) && empty($percent)) return false; if(empty($max)) { $new_width = $info[0] * ($percent/100); $new_height = $info[1] * ($percent/100); } else { if($info[0] < $max && $info[1] < $max) return false; $new_width = ($info[0] > $info[1]) ? $max : ($info[0]/$info[1]) * $max; $new_height = ($info[0] > $info[1]) ? ($info[1]/$info[0]) * $max : $max; } } else { $new_width = $width; $new_height = $height; } $new_image = imagecreatetruecolor($new_width, $new_height); imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $new_width, $new_height, $info[0], $info[1]); $this->image = $new_image; return true; } public function crop($width, $height, $x, $y, $position=NULL) { $backtrace = debug_backtrace(); if(empty($this->image)) return die('<b>Fatal error:</b> Invalid call to Image()->crop, no image is set on <b>line ' . $backtrace[0]['line'] . ' [' . $backtrace[0]['file'] . ']</b>'); $info = Array(imagesx($this->image), imagesy($this->image)); if($width > $info[0] || $height > $info[1]) return false; if(!empty($position) && $position >= 0 && $position <= { switch($position) { case CENTER: $x = ($info[0] - $width)/2; $y = ($info[1] - $height)/2; break; case CENTER_LEFT: $x = 0; $y = ($info[1] - $height)/2; break; case CENTER_RIGHT: $x = ($info[0] - $height); $y = ($info[1] - $height)/2; break; case TOP_CENTER: $x = ($info[0] - $width)/2; $y = 0; break; case TOP_LEFT: $x = 0; $y = 0; break; case TOP_RIGHT: $x = ($info[0] - $width); $y = 0; break; case BOTTOM_CENTER: $x = ($info[0] - $width)/2; $y = ($info[1] - $height); break; case BOTTOM_LEFT: $x = 0; $y = ($info[1] - $height); break; case BOTTOM_RIGHT: $x = ($info[0] - $width); $y = ($info[1] - $height); break; default: return false; break; } } $new_image = imagecreatetruecolor($width, $height); imagecopyresampled($new_image, $this->image, 0, 0, $x, $y, $width, $height, $width, $height); $this->image = $new_image; return true; } public function save($loc, $compression=0) { $backtrace = debug_backtrace(); if(empty($this->image)) return die('<b>Fatal error:</b> Invalid call to Image()->save, no image is set on <b>line ' . $backtrace[0]['line'] . ' [' . $backtrace[0]['file'] . ']</b>'); $func = 'image' . $this->ext; return $func($this->image, $loc, $compression); } public function output() { $backtrace = debug_backtrace(); if(empty($this->image)) return die('<b>Fatal error:</b> Invalid call to Image()->output, no image is set on <b>line ' . $backtrace[0]['line'] . ' [' . $backtrace[0]['file'] . ']</b>'); header('Content-type: image/' . $this->ext); $func = 'image' . $this->ext; $func($this->image); } } For your specific case where you want to resize images to 800X600 you can do something like this: //include class $im = new Image('image..'); $im->resize(800, 600); Quote Link to comment Share on other sites More sharing options...
dittmerdan Posted August 26, 2009 Author Share Posted August 26, 2009 for the resizing portion of your problem you can start with imagecopyresized(). the manual has good documentation on how to use it. thank you this helped. but im still having one problem. <?php $randnumb = rand(000000,999999); mkdir("upload/$randnumb"); $path1= "upload/$randnumb/".$HTTP_POST_FILES['ufile']['name'][0]; copy($HTTP_POST_FILES['ufile']['tmp_name'][0], $path1); echo "<img src=\"$path1\" width=\"150\" height=\"150\">"; $target_width = 800; $target_height = 600; $target_randfold = "/upload/$randnumb"; if (ob_get_level() == 0) ob_start(); if ($handle = opendir("$target_randfold")) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { $destination_path = './$target_randfold/'; $target_path = $destination_path . basename($file); $extension = pathinfo($target_path); $allowed_ext = "jpg, gif, png, bmp, jpeg, JPG"; $extension = $extension[extension]; $allowed_paths = explode(", ", $allowed_ext); $ok = 0; for($i = 0; $i < count($allowed_paths); $i++) { if ($allowed_paths[$i] == "$extension") { $ok = "1"; } } if ($ok == "1") { if($extension == "jpg" || $extension == "jpeg" || $extension == "JPG"){ $tmp_image=imagecreatefromjpeg($target_path); } if($extension == "png") { $tmp_image=imagecreatefrompng($target_path); } if($extension == "gif") { $tmp_image=imagecreatefromgif($target_path); } $width = imagesx($tmp_image); $height = imagesy($tmp_image); //calculate the image ratio $imgratio = ($width / $height); if ($imgratio>1) { $new_width = $target_width; $new_height = ($target_width / $imgratio); } else { $new_height = $target_height; $new_width = ($target_height * $imgratio); } $new_image = imagecreatetruecolor($new_width,$new_height); ImageCopyResized($new_image, $tmp_image,0,0,0,0, $new_width, $new_height, $width, $height); //Grab new image imagejpeg($new_image, $target_path); $image_buffer = ob_get_contents(); ImageDestroy($new_image); ImageDestroy($tmp_image); ob_flush(); flush(); } } } closedir($handle); ob_end_flush(); } ?> i get this error Warning: opendir(/upload/226585) [function.opendir]: failed to open dir: No such file or directory in /home2/dittmerdan/public_html/upload.php on line 30 the problem is right here if ($handle = opendir("$target_randfold")) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { $destination_path = './$target_randfold/'; for some reason any combination i put it will not work. i have messed with this for a few hours. no luck what im doing is creating a random folder using numbers in the upload folder. then resizing the pictures in that folder to 800x600 to save on disk space. the code works when its done like this. $target_width = 800; $target_height = 600; if (ob_get_level() == 0) ob_start(); if ($handle = opendir('upload/')) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { $destination_path = './upload/'; $target_path = $destination_path . basename($file); $extension = pathinfo($target_path); $allowed_ext = "jpg, gif, png, bmp, jpeg, JPG"; $extension = $extension[extension]; $allowed_paths = explode(", ", $allowed_ext); $ok = 0; for($i = 0; $i < count($allowed_paths); $i++) { if ($allowed_paths[$i] == "$extension") { $ok = "1"; } } if ($ok == "1") { if($extension == "jpg" || $extension == "jpeg" || $extension == "JPG"){ $tmp_image=imagecreatefromjpeg($target_path); } if($extension == "png") { $tmp_image=imagecreatefrompng($target_path); } if($extension == "gif") { $tmp_image=imagecreatefromgif($target_path); } $width = imagesx($tmp_image); $height = imagesy($tmp_image); //calculate the image ratio $imgratio = ($width / $height); if ($imgratio>1) { $new_width = $target_width; $new_height = ($target_width / $imgratio); } else { $new_height = $target_height; $new_width = ($target_height * $imgratio); } $new_image = imagecreatetruecolor($new_width,$new_height); ImageCopyResized($new_image, $tmp_image,0,0,0,0, $new_width, $new_height, $width, $height); //Grab new image imagejpeg($new_image, $target_path); $image_buffer = ob_get_contents(); ImageDestroy($new_image); ImageDestroy($tmp_image); ob_flush(); flush(); } } } closedir($handle); ob_end_flush(); } but the photos would have to be in the upload folder. im pulling my hair out and learning at the same time. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.