Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Sunday, October 30, 2016

php resize image on upload

php resize image on upload


I got a form where the user is inserting some data and also uploading an image.

To deal with the image, I got the following code:

 define ("MAX_SIZE","10000");   $errors=0;    $image =$_FILES["fileField"]["name"];          $uploadedfile = $_FILES['fileField']['tmp_name'];          if($image){              $filename = stripslashes($_FILES['fileField']['name']);              $extension = strtolower(getExtension($filename));              if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")){                  echo ' Unknown Image extension ';                  $errors=1;              } else{                  $newname = "$product_cn."."$extension";                  $size=filesize($_FILES['fileField']['tmp_name']);                  if ($size > MAX_SIZE*1024){                   echo "You have exceeded the size limit";                   $errors=1;                  }                  if($extension=="jpg" || $extension=="jpeg" ){                      $uploadedfile = $_FILES['file']['tmp_name'];                      $src = imagecreatefromjpeg($uploadedfile);                  }else if($extension=="png"){                      $uploadedfile = $_FILES['file']['tmp_name'];                      $src = imagecreatefrompng($uploadedfile);                  }else{                      $src = imagecreatefromgif($uploadedfile);                  }                  list($width,$height)=getimagesize($uploadedfile);                    $newwidth=60;                  $newheight=($height/$width)*$newwidth;                  $tmp=imagecreatetruecolor($newwidth,$newheight);                    $newwidth1=25;                  $newheight1=($height/$width)*$newwidth1;                  $tmp1=imagecreatetruecolor($newwidth1,$newheight1);                    imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight, $width,$height);                    imagecopyresampled($tmp1,$src,0,0,0,0,$newwidth1,$newheight1, $width,$height);                    $filename = "../products_images/".$newname;                  $filename1 = "../products_images/thumbs/".$newname;                    imagejpeg($tmp,$filename,100); //file name also indicates the folder where to save it to                  imagejpeg($tmp1,$filename1,100);                    imagedestroy($src);                  imagedestroy($tmp);                  imagedestroy($tmp1);                }           }  

getExtension function:

function getExtension($str) {             $i = strrpos($str,".");           if (!$i) { return ""; }            $l = strlen($str) - $i;           $ext = substr($str,$i+1,$l);           return $ext;   }  

I've wrote some notation in the code since im not really familiar with those functions.

for some reason, it doesn't work.

when I'm going to the folder "product_images" OR "product_images/thumbs" I can't find any image uploaded.

Any idea what's wrong with my code? there should be 60px width image, and 25px width image.

NOTE:

variables that you don't know where they were declared such as $product_cn were declared before that block of code which works prefectly fine(tested it). If you still want a glance at it, feel free to ask for the code.

Thanks in advance!

Answer by Alok M. Kejriwal for php resize image on upload


You can use this library to manipulate the image while uploading. http://www.verot.net/php_class_upload.htm

Answer by zeusstl for php resize image on upload


Here is another nice and easy solution:

        $maxDim = 800;          list($width, $height, $type, $attr) = getimagesize( $_FILES['myFile']['tmp_name'] );          if ( $width > $maxDim || $height > $maxDim ) {              $target_filename = $_FILES['myFile']['tmp_name'];              $fn = $_FILES['myFile']['tmp_name'];             
$size = getimagesize( $fn ); $ratio = $size[0]/$size[1]; // width/height if( $ratio > 1) { $width = $maxDim; $height = $maxDim/$ratio; } else { $width = $maxDim*$ratio; $height = $maxDim; } $src = imagecreatefromstring( file_get_contents( $fn ) ); $dst = imagecreatetruecolor( $width, $height ); imagecopyresampled( $dst, $src, 0, 0, 0, 0, $width, $height, $size[0], $size[1] ); imagedestroy( $src ); imagepng( $dst, $target_filename ); // adjust format as needed imagedestroy( $dst ); }

Reference:

PHP resize image proportionally with max width or weight

Answer by Pankaj Upadhyay for php resize image on upload


Download library file Zebra_Image.php belo link

https://drive.google.com/file/d/0Bx-7K3oajNTRV1I2UzYySGZFd3M/view

resizeimage.php

source_path = $target_file1;      $ext = $photo;      // indicate a target image      $resize_image->target_path = 'images/thumbnil/' . $ext;      // resize      // and if there is an error, show the error message      if (!$resize_image->resize(200, 200, ZEBRA_IMAGE_NOT_BOXED, -1));      // from this moment on, work on the resized image      $resize_image->source_path = 'images/thumbnil/' . $ext;  ?>  

Answer by ziiweb for php resize image on upload


You can use Imagine library also. It uses GD and Imagick.

Answer by Edwinfad for php resize image on upload


// This was my example that I used to automatically resize every inserted photo to 100 by 50 pixel and image format to jpeg hope this helps too

if($result){  $maxDimW = 100;  $maxDimH = 50;  list($width, $height, $type, $attr) = getimagesize( $_FILES['photo']['tmp_name'] );  if ( $width > $maxDimW || $height > $maxDimH ) {      $target_filename = $_FILES['photo']['tmp_name'];      $fn = $_FILES['photo']['tmp_name'];      $size = getimagesize( $fn );      $ratio = $size[0]/$size[1]; // width/height      if( $ratio > 1) {          $width = $maxDimW;          $height = $maxDimH/$ratio;      } else {          $width = $maxDimW*$ratio;          $height = $maxDimH;      }      $src = imagecreatefromstring(file_get_contents($fn));      $dst = imagecreatetruecolor( $width, $height );      imagecopyresampled($dst, $src, 0, 0, 0, 0, $width, $height, $size[0], $size[1] );        imagejpeg($dst, $target_filename); // adjust format as needed      }    move_uploaded_file($_FILES['pdf']['tmp_name'],"pdf/".$_FILES['pdf']['name']);  

Answer by HoldOffHunger for php resize image on upload


If you want to use Imagick out of the box (included with most PHP distributions), it's as easy as...

$image = new Imagick();  $image_filehandle = fopen('some/file.jpg', 'a+');  $image->readImageFile($image_filehandle );    $image->scaleImage(100,200,FALSE);    $image_icon_filehandle = fopen('some/file-icon.jpg', 'a+');  $image->writeImageFile($image_icon_filehandle);  

You will probably want to calculate width and height more dynamically based on the original image. You can get an image's current width and height, using the above example, with $image->getImageHeight(); and $image->getImageWidth();.


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

Related Posts:

0 comments:

Post a Comment

Popular Posts

Fun Page

Powered by Blogger.