Jump to content

[SOLVED] Image Upload & Resize


rik72

Recommended Posts

This is intended to Upload 1 image; resize it 3 times and generate a random name for the image and save it.  It's sometimes showing a blank page, sometimes showing Server Misconfiguration Error; any help is appreciated on fixing this.

 

To be honest, i think im over doing it and using too much code, and some places not enough, but god knows!

 

<?php

$totalChar = 15;
$salt = "abcdefghijklmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ123456789";
srand((double)microtime()*1000000); 
$pass="";
for ($i=0;$i<$totalChar;$i++)
$pass = $pass . substr ($salt, rand() % strlen($salt), 1);

$uploadedfile1 = $_FILES['uploadfile']['tmp_name'];
$uploadedfile2 = $_FILES['uploadfile']['tmp_name'];
$uploadedfile3 = $_FILES['uploadfile']['tmp_name'];

$src1 = touch($uploadedfile1);
$src2 = touch($uploadedfile2);
$src3 = touch($uploadedfile3);

list($width1,$height1)=getimagesize($uploadedfile1);
list($width2,$height2)=getimagesize($uploadedfile2);
list($width3,$height3)=getimagesize($uploadedfile3);

$newwidth1=600;
$newwidth2=200;
$newwidth3=100;
$newheight1=($height1/$width1)*600;
$newheight2=($height2/$width2)*200;
$newheight3=($height3/$width3)*100;

$tmp1=imagecreatetruecolor($newwidth1,$newheight1);
$tmp2=imagecreatetruecolor($newwidth2,$newheight2);
$tmp3=imagecreatetruecolor($newwidth3,$newheight3);

imagecopyresampled($tmp1,$src1,0,0,0,0,$newwidth1,$newheight1,$width1,$height1);
imagecopyresampled($tmp2,$src2,0,0,0,0,$newwidth2,$newheight2,$width2,$height2);
imagecopyresampled($tmp3,$src3,0,0,0,0,$newwidth3,$newheight3,$width3,$height3);

$filename1 = "upimg/$pass" . $_FILES["file"]["name"];
$filename2 = "thimg/$pass" . $_FILES["file"]["name"];
$filename3 = "avimg/$pass" . $_FILES["file"]["name"];
$name = $_FILES["file"]["name"];
imagejpeg($tmp1,$filename1,100);
imagejpeg($tmp2,$filename2,100);
imagejpeg($tmp3,$filename3,100);

imagedestroy($src1);
imagedestroy($tmp1);

imagedestroy($src2);
imagedestroy($tmp2);

imagedestroy($src3);
imagedestroy($tmp3);

echo "$pass $name";
?>

 

Link to comment
Share on other sites

Heres a cleaned up version

UNTESTED,

it should save memory, basically it resized to the largest size then resizes the last one to next largest size, etc, this save it resizing one large image each time.. also i have broken it down to clean up as it goes..

i removed the touch as i can see how that would of worked, as your using imagejpeg, i used imagecreatefromjpeg, which mean this will only work with jpegs..

 

i hope it makes sense

 

<?php

$totalChar = 15;
$salt = "abcdefghijklmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ123456789";
srand((double)microtime()*1000000); 
$pass="";
for ($i=0;$i<$totalChar;$i++)
$pass = $pass . substr ($salt, rand() % strlen($salt), 1);

$uploadedfile = $_FILES['uploadfile']['tmp_name'];

list($width,$height)=getimagesize($uploadedfile);

$newwidth1=600;
$newwidth2=200;
$newwidth3=100;
$newheight1=($height/$width)*600;
$newheight2=($height/$width)*200;
$newheight3=($height/$width)*100;

#1
$src = imagecreatefromjpeg($uploadedfile);//Create from JPEG!

$tmp1=imagecreatetruecolor($newwidth1,$newheight1);
imagecopyresampled($tmp1,$src,0,0,0,0,$newwidth1,$newheight1,$width,$height);
$filename = "upimg/$pass" . $_FILES["file"]["name"];
imagejpeg($tmp1,$filename,100);
imagedestroy($src);

#2
$tmp2=imagecreatetruecolor($newwidth2,$newheight2);
imagecopyresampled($tmp2,$tmp1,0,0,0,0,$newwidth2,$newheight2,$newwidth1,$newheight1);
$filename = "thimg/$pass" . $_FILES["file"]["name"];
imagejpeg($tmp2,$filename,100);
imagedestroy($tmp1);

#3
$tmp3=imagecreatetruecolor($newwidth3,$newheight3);
imagecopyresampled($tmp3,$tmp2,0,0,0,0,$newwidth3,$newheight3,$newwidth2,$newheight2);
$filename = "avimg/$pass" . $_FILES["file"]["name"];
imagejpeg($tmp3,$filename,100);
imagedestroy($tmp2);
imagedestroy($tmp3);

$name = $_FILES["file"]["name"];
echo "$pass $name";
?>

Link to comment
Share on other sites

quite a few updates again untested,

 

<?php
$fileName = $_FILES["file"]["name"];
// get the file extension first
$ext = substr(strrchr($fileName, "."), 1);
// make the random file name
do{
$randName = md5(rand() * time());
$pass = "$randName.$ext";
$Check = (file_exists("upimg/$pass") || file_exists("thimg/$pass") || file_exists("avimg/$pass"));
}while( $Check )

$uploadedfile = $_FILES['uploadfile']['tmp_name'];

list($width,$height)=getimagesize($uploadedfile);

$newwidth1=600;
$newwidth2=200;
$newwidth3=100;
$newheight1=($height/$width)*600;
$newheight2=($height/$width)*200;
$newheight3=($height/$width)*100;

switch($_FILES['uploadfile']['type'])
{
case "image/gif":
	$src = imagecreatefromgif($uploadedfile);//Create from GIF!
	$type = "gif";
break;
case "image/jpg":
case "image/jpeg":
	$src = imagecreatefromjpeg($uploadedfile);//Create from JPEG!
	$type = "jpg";
break;
}
#1
//$src = imagecreatefromjpeg($uploadedfile);//Create from JPEG!

$tmp1=imagecreatetruecolor($newwidth1,$newheight1);
imagecopyresampled($tmp1,$src,0,0,0,0,$newwidth1,$newheight1,$width,$height);
$filename = "upimg/$pass";// . $_FILES["file"]["name"];
if($type == "gif")
{
imagegif($tmp1,$filename,100);
}elseif($type == "jpg"){
imagejpeg($tmp1,$filename,100);
}
imagedestroy($src);

#2
$tmp2=imagecreatetruecolor($newwidth2,$newheight2);
imagecopyresampled($tmp2,$tmp1,0,0,0,0,$newwidth2,$newheight2,$newwidth1,$newheight1);
$filename = "thimg/$pass";// . $_FILES["file"]["name"];
if($type == "gif")
{
imagegif($tmp2,$filename,100);
}elseif($type == "jpg"){
imagejpeg($tmp2,$filename,100);
}
imagedestroy($tmp1);

#3
$tmp3=imagecreatetruecolor($newwidth3,$newheight3);
imagecopyresampled($tmp3,$tmp2,0,0,0,0,$newwidth3,$newheight3,$newwidth2,$newheight2);
$filename = "avimg/$pass";// . $_FILES["file"]["name"];
if($type == "gif")
{
imagegif($tmp3,$filename,100);
}elseif($type == "jpg"){
imagejpeg($tmp3,$filename,100);
}
imagedestroy($tmp2);
imagedestroy($tmp3);

//$name = $_FILES["file"]["name"];
echo "$pass"; // $name";
?>

Link to comment
Share on other sites

Parse error: syntax error, unexpected T_VARIABLE, expecting ';'

 

I'm getting this error, yet see no issues myself..

 

Its coming on line 22, which is this:

 

$Check = (file_exists("upimg/$pass") || file_exists("thimg/$pass") || file_exists("avimg/$pass"));
}while( $Check )
$uploadedfile = $_FILES["uploadfile"]["tmp_name"];

 

The center line is line 22

 

Thanks

 

Rik

 

Link to comment
Share on other sites

Sorry, so simple to fix! but now getting these when uploading gif;(JPG works fine!)

 

Warning: imagecreatefromjpeg() [function.imagecreatefromjpeg]: gd-jpeg: JPEG library reports unrecoverable error: in *

 

Warning: imagecreatefromjpeg() [function.imagecreatefromjpeg]: '/var/tmp/php69wIuG' is not a valid JPEG file in *

 

Warning: imagecopyresampled(): supplied argument is not a valid Image resource in *

 

Warning: imagedestroy(): supplied argument is not a valid Image resource in *

Link to comment
Share on other sites

$fileName = $_FILES["uploadfile"]["name"];
// get the file extension first
$ext = substr(strrchr($fileName, "."), 1);
// make the random file name
do{
$randName = md5(rand() * time());
$pass = "$randName.$ext";
$Check = (file_exists("upimg/$pass") || file_exists("thimg/$pass") || file_exists("avimg/$pass"));
}while( $Check );
$uploadedfile = $_FILES["uploadfile"]["tmp_name"];

list($width,$height)=getimagesize($uploadedfile);

$newwidth1=600;
$newwidth2=200;
$newwidth3=100;
$newheight1=($height/$width)*600;
$newheight2=($height/$width)*200;
$newheight3=($height/$width)*100;

switch($_FILES['uploadfile']['type'])
{
case "image/gif":
	$src = imagecreatefromgif($uploadedfile);//Create from GIF!
	$type = "gif";
break;
case "image/jpg":
case "image/jpeg":
	$src = imagecreatefromjpeg($uploadedfile);//Create from JPEG!
	$type = "jpg";
break;
}
#1
$src = imagecreatefromjpeg($uploadedfile);//Create from JPEG!
$tmp1=imagecreatetruecolor($newwidth1,$newheight1);
imagecopyresampled($tmp1,$src,0,0,0,0,$newwidth1,$newheight1,$width,$height);
$filename = "upimg/$pass";// . $_FILES["uploadfile"]["name"];
if($type == "gif")
{
imagegif($tmp1,$filename,100);
}elseif($type == "jpg"){
imagejpeg($tmp1,$filename,100);
}
imagedestroy($src);

#2
$tmp2=imagecreatetruecolor($newwidth2,$newheight2);
imagecopyresampled($tmp2,$tmp1,0,0,0,0,$newwidth2,$newheight2,$newwidth1,$newheight1);
$filename = "thimg/$pass";// . $_FILES["uploadfile"]["name"];
if($type == "gif")
{
imagegif($tmp2,$filename,100);
}elseif($type == "jpg"){
imagejpeg($tmp2,$filename,100);
}
imagedestroy($tmp1);

#3
$tmp3=imagecreatetruecolor($newwidth3,$newheight3);
imagecopyresampled($tmp3,$tmp2,0,0,0,0,$newwidth3,$newheight3,$newwidth2,$newheight2);
$filename = "avimg/$pass";// . $_FILES["uploadfile"]["name"];
if($type == "gif")
{
imagegif($tmp3,$filename,100);
}elseif($type == "jpg"){
imagejpeg($tmp3,$filename,100);
}
imagedestroy($tmp2);
imagedestroy($tmp3);

$name = $_FILES["uploadfile"]["name"];
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.