resize width and compress images on upload php mysql -


i have client sends me text messages iphone images me upload gallery. i'm trying create admin system can take images texts, go admin page on iphone , upload images straight gallery.

this save me tons of time in day day work schedule.

using provided code. how can add following functions:

  1. i compress file size down smaller size if possible, similar save web jpg function in photoshop. (most images around 1-3 mb. them down around 150-500kb max)

  2. i automatically change width 760px, keep aspect ratio images not squished. sends me landscape , portrait images.

  3. beings iphone images. have extension .jpg (all caps) change .jpg (all lower case.) not deal breaker know how future use.

either 1 of these functions helpful, 3 ideal situation.

here code i'm working with?

this final correct code uploading , resizing images provided @tman make sure have imagick installed in php.ini file. check hosting provider install it.

<?php include($_server['document_root'] . "/connections/dbconnect.php");    for($i=0;$i<count($_files["image"]["name"]);$i++){ if($_files["image"]["name"][$i] != ''){ // don't insert if file name empty $datatype = mysql_real_escape_string($_post["datatype"][$i]); $title = mysql_real_escape_string($_post["title"][$i]);  $filedata = pathinfo($_files["image"]["name"][$i]); $filename = uniqid() . '.' . $filedata['extension']; $target_path = $_server['document_root'] . "/images/gallery/" . $filename;  if (move_uploaded_file($_files["image"]["tmp_name"][$i], $target_path)){ // file in images/gallery folder. // insert record database executing following query: $sql="insert images (data_type, title, file_name) "."values('$datatype','$title','$filename')"; $retval = mysql_query($sql);   ///new  $size = getimagesize($target_path); $width=$size[0];  $height=$size[1];  $newwidth = 760; $newheight = $height*($newwidth/$width); $pic = new imagick($target_path);//specify name $pic->resizeimage($newwidth,$newhight,imagick::filter_lanczos,1); unlink($target_path); $pic->writeimage($target_path); $pic->destroy(); ///new   echo "the image {$_files['image']['name'][$i]} uploaded , added gallery<br /> <a href='index.php'>add image</a><br />"; } else { echo "there error uploading file {$_files['image']['name'][$i]}, please try again!<br />"; } } } // close foreach ?> 

uploader.php original code. allows me upload 4 images @ once. works!!!

<?php include($_server['document_root'] . "/connections/dbconnect.php");  for($i=0;$i<count($_files["image"]["name"]);$i++){   if($_files["image"]["name"][$i] != ''){ // don't insert if file name empty     $datatype = mysql_real_escape_string($_post["datatype"][$i]);     $title = mysql_real_escape_string($_post["title"][$i]);      $filedata = pathinfo($_files["image"]["name"][$i]);     $filename = uniqid() . '.' . $filedata['extension'];     $target_path = $_server['document_root'] . "/images/gallery/" . $filename;    if (move_uploaded_file($_files["image"]["tmp_name"][$i], $target_path)){    // file in images/gallery folder.      // insert record database executing following query:      $sql="insert images (data_type, title, file_name) "."values('$datatype','$title','$filename')";      $retval = mysql_query($sql);      echo "the image {$_files['image']['name'][$i]} uploaded , added gallery<br />      <a href='index.php'>add image</a><br />";   }   else   {    echo "there error uploading file {$_files['image']['name'][$i]}, please try again!<br />";     }   } } // close foreach ?> 

fyi, allow give unique names images, resize width, keep correct aspect ratio , upload multiple file @ same time.

awesome stuff!

like this:

$filelocation='http://help.com/images/help.jpg'; $newfilelocation='http://help.com/images/help1.jpg';  $size = getimagesize($filelocation); $width=$size[0];//might need ['1'] im tired .. :) $height=$size[1]; // plz note im not sure of units pixles? & have width , height   confused //just had knee surgery im kinda loopy :)  $newwidth = 760; $newheight = $height*($newwidth/$width)     $pic = new imagick( $filelocation);//specify name  $pic->resizeimage($newwidth,$newhight,imagick::filter_lanczos,1);  //again might have width , heing confused  $pic->writeimage($newfilelocation);//output name  $pic->destroy();  unlink($filelocation);//deletes image 

Comments