How to compress multiple images size while uploading imaged using php? -
below code tried there showing warning errors:
warning: getimagesize(c:\xampp\tmp\php2f0b.tmp): failed open stream: no such file or directory in c:\xampp\htdocs\maahima\admin\uploadbulkimages.php on line 55
warning: imagejpeg() expects parameter 1 resource, string given in c:\xampp\htdocs\maahima\admin\uploadbulkimages.php on line 66
i want code insert images in folder in bulk compressed image.
<?php if(isset($_files['file']['tmp_name'])){ $pro_image = $_files['file']['tmp_name']; $no_of_file = count($_files['file']['tmp_name']); $name = ''; $type = ''; $size = ''; $error = ''; for($i=0;$i < $no_of_file;$i++){ if(! is_uploaded_file($_files['file']['tmp_name'][$i])){ header("location:add_products.php?error=er8r5r"); } else{ if(move_uploaded_file($_files['file']['tmp_name'][$i],"../products/".$_files['file']['name'][$i])){ if ($_files["file"]["error"][$i] > 0) { $error = $_files["file"]["error"]; } else { $image=""; $error = "uploaded image should jpg or gif or png"; $url = '../smallsize-image/'.$_files['file']['name'][$i]; echo $url; $source_url=$_files["file"]["tmp_name"][$i]; $destination_url=$url; $quality=12; $info = getimagesize($source_url); if ($info['mime'] == 'image/jpeg') $image = imagecreatefromjpeg($source_url); elseif ($info['mime'] == 'image/gif') $image = imagecreatefromgif($source_url); elseif ($info['mime'] == 'image/png') $image = imagecreatefrompng($source_url); imagejpeg($image, $destination_url, $quality); return $destination_url; $buffer = file_get_contents($url); /* force download dialog... */ //header("content-type: application/force-download"); //header("content-type: application/octet-stream"); //header("content-type: application/download"); // header("location:add_products.php?succcess=s8sc5s"); /* don't allow caching... */ // header("cache-control: must-revalidate, post-check=0, pre-check=0"); /* set data type, size , filename */ //header("content-type: application/octet-stream"); //header("content-length: " . strlen($buffer)); //header("content-disposition: attachment; filename=$url"); /* send our file... */ echo $buffer; } } else{ header("location:add_products.php?error=er8r5r"); } } } } else{ } ?>
your script has lot of illogical features. if going redirect, need exit
. should not have return
in middle of script, random (probably unfinished script , it's inside function/method?). here things if this.
if don't have config has absolute defines, create 1 , include on every initial load page @ top.
this tested , works, provided it's implemented correctly , demonstrated. use of it, bits of it, or none of it, work:
/config.php
<?php define('ds',directory_separator); define('root_dir',__dir__); define('upload_dir',root_dir.ds.'uploads'.ds.'images'); define('thumb_dir',root_dir.ds.'uploads'.ds.'thumbs'); define('vendor_dir',root_dir.ds.'vendors');
i consider making quick functions, in case class/method system complete simple tasks.
/vendors/files.php
class files { /* ** @description turn numbered $_files array normal 1 */ public static function normalize($key = 'file') { foreach($_files[$key]['tmp_name'] $fkey => $value) { $files[] = array( 'tmp_name'=>$_files[$key]['tmp_name'][$fkey], 'name'=>$_files[$key]['name'][$fkey], 'error'=>$_files[$key]['error'][$fkey], 'size'=>$_files[$key]['size'][$fkey], 'type'=>$_files[$key]['type'][$fkey] ); } return $files; } /* ** @description contain step editable method ** ease of use , reuse. */ public static function compress($path, $dest, $quality = 12) { $info = getimagesize($path); if($info['mime'] == 'image/jpeg') $image = imagecreatefromjpeg($path); elseif($info['mime'] == 'image/gif') $image = imagecreatefromgif($path); elseif($info['mime'] == 'image/png') $image = imagecreatefrompng($path); imagejpeg($image,$dest,$quality); } }
put together, in case maybe throw exceptions , save errors session. loop through them on add_products.php
page.
# add our config include(__dir__.directory_separator.'config.php'); # add our file handler class (spl_autoload_register() better option) include(vendor_dir.ds.'files.php'); # observe file upload if(!empty($_files['file']['tmp_name'])){ # use our handy convert method $files = files::normalize(); # try here try { # loop through our normalized file array foreach($files $i => $file){ # throw error exception here if(!is_uploaded_file($file['tmp_name'])) throw new exception('file not uploaded document.'); # add create directory line or 2 cover bases if(!is_dir(upload_dir)) { if(!mkdir(upload_dir,0755,true)) throw new exception('upload folder not created.'); } # ditto if(!is_dir(thumb_dir)) { if(!mkdir(thumb_dir,0755,true)) throw new exception('thumb folder not created.'); } # create absolute path full size upload $path = upload_dir.ds.$file['name']; # if no errors , file moved properly, compress , save thumb if(($file["error"]) == 0 && move_uploaded_file($file['tmp_name'],$path)) # create thumbnail from/after file moved, not temp file files::compress($path,thumb_dir.ds.$file['name']); # throw error on fail else throw new exception('an unknown upload error occurred.'); } } # catch, assign, redirect catch (exception $e) { $_session['errors'][] = $e->getmessage(); header("location: add_products.php?error=er8r5r"); exit; } }
Comments
Post a Comment