Jump to content

PHP Image Resize


imperium2335

Recommended Posts

Hi, I've made the following but it doesn't seem to be working at all, could someone help me out? Also is there any way to make this more efficient? Its supposed to take the image the person uploaded, validate it, then resize it to make a thumbnail and save that thumbnail in a directory, later the same image will be resized again but as a bigger version (like a gallery).

 

Thanks in advance.

 

<?PHP
$updir = $_POST['userfile'] ;
$ext = substr($updir, strrpos($updir, '.') + 1);
$isize = getimagesize($updir) ;
$width = $isize[0] ;
$height = $isize[1] ;
if($ext != "jpg" || $ext != "jpeg" || $ext != "jif" || $ext != "gif" || $ext != "png" || $ext != "bmp" || $ext != "tif" || $ext != "tiff" || $ext != "pdf" || $ext != "jp2" || $ext != "jpx"){
array_push($ERR_MESSAGE, "Invalid or unrecognised file format. Image must be a known image format, such as BMP or JPG etc") ;
$ERRORS = TRUE ;
}
elseif(filesize($updir) > 5000000){
array_push($ERR_MESSAGE, "File to big. Filesize cannot exceed 5MB.") ;
$ERRORS = TRUE ;
}
elseif($width < 250 || $height < 300) {
array_push($ERR_MESSAGE, "Your image is to small, it must be atleast 250 by 300 pixels.") ;
$ERRORS = TRUE ;
}

/////END FILE VALIDATION

if($ERRORS = FALSE) {
//Creat the thumbnail...
$updir = "user-cake-photos/" ;
$thumbs = "home-made-cake-pictures/thumbnails/" ;
$imgdir = "home-made-cake-pictures/" ;
$updir = $updir . basename($_FILES['userfile']['name']) ;

$twidth = 108 ;
$new_height = round(($height * $twidth) / $width);
for ($i = 0; $new_height > 81 ; $twidth--  { //If the height is to big then continuously shrink the image until it isn't to tall.
$new_height = round(($height * $twidth) / $width);
}
$resizedimage = imagecreatetruecolor($twidth, $new_height) ;
$placeholder = imagecreatefromjpeg($updir) ;
imagecopyresampled($resizedimage, $placeholder, 0, 0, 0, 0, $twidth, $new_height, $width, $height) ;

imagejpeg($resizedimage, $updir, 80) ;
imagedestroy($resizedimage) ;

copy($updir, "$thumbs" . $_FILES['userfile']['name']) ;
?>

Link to comment
https://forums.phpfreaks.com/topic/176169-php-image-resize/
Share on other sites

It says it cant move the file because it doesn't exist on the server, this is what i have now (just the file transfer bit):

$updir = "user-cake-photos/" ;
$updir = $updir . basename($_FILES['userfile']['name']) ;
if(move_uploaded_file($_FILES['userfile']['tmp_name'], $updir))
{
    echo "The file " . basename($_FILES['userfile']['name']). 
    " has been uploaded";
}else{echo "File upload fail." ;}

$ext = substr($updir, strrpos($updir, '.') + 1);
echo "File is called: $updir <br />" ;
echo "Extension is: $ext <br />" ;
$isize = getimagesize($updir) ;
$width = $isize[0] ;
$height = $isize[1] ;
if($ext != "jpg" || $ext != "jpeg" || $ext != "jif" || $ext != "gif" || $ext != "png" || $ext != "bmp" || $ext != "tif" || $ext != "tiff" || $ext != "pdf" || $ext != "jp2" || $ext != "jpx"){
array_push($ERR_MESSAGE, "Invalid or unrecognised file format. Image must be a known image format, such as BMP or JPG etc") ;
$ERRORS = TRUE ;
}
elseif(filesize($updir) > 5000000){
array_push($ERR_MESSAGE, "File to big. Filesize cannot exceed 5MB.") ;
$ERRORS = TRUE ;
}
elseif($width < 250 || $height < 300) {
array_push($ERR_MESSAGE, "Your image is to small, it must be atleast 250 by 300 pixels.") ;
$ERRORS = TRUE ;
}

 

The error messages are:

 

Warning: move_uploaded_file(photos/china-chinese-sleeping-in-internet-bar-05-560x420.jpg) [function.move-uploaded-file]: failed to open stream: No such file or directory in /xxxxxxxx/xxxxxxx/xxxxxxxxx/php/upload.php on line 44

 

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phpmxtIKg' to 'photos/china-chinese-sleeping-in-internet-bar-05-560x420.jpg' in /xxxxxx/xxxxxxx/xxxxxxxxx/php/upload.php on line 44

File upload fail.File is called: photos/china-chinese-sleeping-in-internet-bar-05-560x420.jpg

Extension is: jpg

 

Warning: getimagesize(photos/china-chinese-sleeping-in-internet-bar-05-560x420.jpg) [function.getimagesize]: failed to open stream: No such file or directory in /xxxx/xxxxxxx/xxxxxx/php/upload.php on line 53

Link to comment
https://forums.phpfreaks.com/topic/176169-php-image-resize/#findComment-928821
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.