Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php resize image on upload

Tags:

php

image

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 I'm 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). If you still want a glance at it, feel free to ask for the code.

Thanks in advance!

like image 874
kfirba Avatar asked Sep 14 '13 19:09

kfirba


People also ask

How do I scale an image in PHP?

The imagescale() function is an inbuilt function in PHP which is used to scale an image using the given new width and height. Parameters: This function accepts four parameters as mentioned above and described below: $image: It is returned by one of the image creation functions, such as imagecreatetruecolor().


1 Answers

Here is another nice and easy solution:

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

Reference: PHP resize image proportionally with max width or weight

Edit: Cleaned up and simplified the code a bit. Thanks @jan-mirus for your comment.

like image 195
zeusstl Avatar answered Oct 13 '22 04:10

zeusstl