kfirba kfirba - 10 months ago 58
PHP Question

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");

$image =$_FILES["fileField"]["name"];
$uploadedfile = $_FILES['fileField']['tmp_name'];
$filename = stripslashes($_FILES['fileField']['name']);
$extension = strtolower(getExtension($filename));
if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")){
echo ' Unknown Image extension ';
} else{
$newname = "$product_cn."."$extension";
if ($size > MAX_SIZE*1024){
echo "You have exceeded the size limit";
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);
$src = imagecreatefromgif($uploadedfile);



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



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.


variables that you don't know where they were declared such as
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 Source

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