...

View Full Version : Image upload nightmare!



greens85
12-03-2010, 02:43 PM
Hi all,

I currently have an image upload/resize script but the thumbnails generated are terrible!

I thought it might be easier to find an existing script that could do this... I've taken a look at this one:

http://www.blazonry.com/scripting/upload-size.php

But when I download it and make changes etc it always fails saying it is not JPEG (although it certainly is) and:

Warning: unlink() [function.unlink]: No such file or directory in

Although I would assume the latter is because it wont upload in the first place!

Can anyone link me to any good image upload/resize scripts that generate good quality thumbnails that are limited to certain dimensions?

Any help would be appreciated as this is driving me mad!

Many thanks,

Greens85

MattF
12-03-2010, 04:12 PM
Used as:



$newimage = image_resize($file, $width, $height);


returns an array with the info for the newly created image.



function image_resize($file, $width, $height, $delete = true)
{
$image = getimagesize($file);
$image['type'] = $image[2];
$image['width'] = $image[0];
$image['height'] = $image[1];

if (!$image)
{
return false;
}

/***********************************/

$ratio = ($image['width']/$image['height']);
$image['file'] = $file.time();

if ($image['width'] <= $width && $image['height'] <= $height)
{
copy($file, $image['file']);

if (is_file($image['file']))
{
if ($delete && is_file($file))
{
unlink($file);
}
$image['name'] = basename($image['file']);

return $image;
}
return false;
}
else
{
if (($width/$height) > $ratio)
{
$width = ($height*$ratio);
}
else
{
$height = ($width/$ratio);
}

/***********************************/

$dimg = imagecreatetruecolor($width, $height);
$background = imagecolorallocate($dimg, 255, 255, 255);
imagefill($dimg, 0, 0, $background);

/***********************************/

if ($image['type'] == 1)
{
$simg = imagecreatefromgif($file);
imagecopyresampled(
$dimg, $simg, 0, 0, 0, 0, $width,
$height, $image['width'], $image['height']
);
imagegif($dimg, $image['file']);
}
else if ($image['type'] == 2)
{
$simg = imagecreatefromjpeg($file);
imagecopyresampled(
$dimg, $simg, 0, 0, 0, 0, $width,
$height, $image['width'], $image['height']
);
imagejpeg($dimg, $image['file'], 85);
}
else if ($image['type'] == 3)
{
$simg = imagecreatefrompng($file);
imagealphablending($simg, true);
imagesavealpha($simg, true);
imagecopyresampled(
$dimg, $simg, 0, 0, 0, 0, $width,
$height, $image['width'], $image['height']
);
imagepng($dimg, $image['file'], 9);
}
imagedestroy($dimg);
imagedestroy($simg);

/***********************************/

if (is_file($image['file']))
{
$image['name'] = basename($image['file']);
list($image['width'], $image['height']) = getimagesize($image['file']);

if ($delete && is_file($file))
{
unlink($file);
}
return $image;
}
return false;
}
}

greens85
12-03-2010, 04:28 PM
Hi Matt,

Thanks for this!

How would this handle portrait and landscape images? I have found that mine and many of the others I have tried on the net dont display portrait images too bad but aren't too clever at handling landscape images.

Is it possible to set specific dimensions with this?

Many thanks.

MattF
12-03-2010, 04:38 PM
Width and height set the maximum dimensions allowed. If the original image is above those sizes, it will be proportionately scaled and reduced, hence yes, it should be fine with both portrait and landscape. Try it and then you'll know for definite. Example:



Portrait:

$newimage = image_resize($file, 600, 800);

Landscape:

$newimage = image_resize($file, 800, 600);

greens85
12-03-2010, 05:53 PM
Width and height set the maximum dimensions allowed. If the original image is above those sizes, it will be proportionately scaled and reduced, hence yes, it should be fine with both portrait and landscape. Try it and then you'll know for definite. Example:



Portrait:

$newimage = image_resize($file, 600, 800);

Landscape:

$newimage = image_resize($file, 800, 600);


Thanks for this Matt,

I will give it a shot and let you know...

Much appreciated.

MattF
12-03-2010, 06:12 PM
Just made a slight alteration to the function above, (added lines 4, 5 and 6 and changed lines 1 and 3), so use the updated version. This is an example of it in use:

http://gxcr.org/devtest/

Click on the thumbnails and you'll see the original images they've been created from, via that function.

greens85
12-06-2010, 10:31 AM
Just made a slight alteration to the function above, (added lines 4, 5 and 6 and changed lines 1 and 3), so use the updated version. This is an example of it in use:

http://gxcr.org/devtest/

Click on the thumbnails and you'll see the original images they've been created from, via that function.

Hi Matt,

Sorry for the delayed response to this!

I've just tried to test out your script but I got a 404 error... I assume you have since taken the script down?

Cheers,

Greens85

MattF
12-06-2010, 12:47 PM
Temporarily. It'll be back within the hour. It's my test site, so tends to disappear and reappear on occasion whilst I'm reinstalling.

greens85
12-06-2010, 01:15 PM
Temporarily. It'll be back within the hour. It's my test site, so tends to disappear and reappear on occasion whilst I'm reinstalling.

Ah I see... I have started implementing the script but I am having a few problems... probably more so to do with my lack of knowledge than your script!

I am using this to upload and move my "big image":


<?php
include("../functions/resize_image.php");

$file_ext = strrchr($_FILES['imagefile']['name'], '.'); // Get The File Extention In The Format Of , For Instance, .jpg, .gif or .php

$image_name=time().$file_ext;
$newname="images/".$image_name;

$copy = copy($_FILES['imagefile']['tmp_name'], $newname); // Move Image From TempLocation To Permanent Location

$newimage = image_resize($newname, 250, 250);
?>

However what I find is that the image is getting renamed twice and the final result is something like:

100_0497.JPG1291637659

I can see why this is happening, basically I am renaming and so are you in the function. However if I try and remove the naming part on mine it breaks the upload and copy().

Any chance you could advise me here?

many thanks.

MattF
12-06-2010, 01:44 PM
<?php
include("../functions/resize_image.php");

$file_ext = strrchr($_FILES['imagefile']['name'], '.'); // Get The File Extention In The Format Of , For Instance, .jpg, .gif or .php

$image_name=time().$file_ext;
$newname="images/".$image_name;

if (is_uploaded_file($_FILES['imagefile']['tmp_name']))
{
$newimage = image_resize($_FILES['imagefile']['tmp_name'], 250, 250);
rename($newimage['file'], $newname);
}
?>

greens85
12-06-2010, 02:05 PM
Hi matt,

Many thanks for that.. works a treat.

I assume that the image now sitting in images/ is the resized image?

As I need a full size image to be saved aswell, I guess thats just a case of using move_uploaded_file() before the resizing takes place?

Then redirect the resized image to the thumbs folder instead of the images/ folder?

As for portrait and landscape, I have tried both and both look 100% better, so many many thanks for your great function!

many thanks,

greens85

MattF
12-06-2010, 02:22 PM
<?php
include("../functions/resize_image.php");

$file_ext = strrchr($_FILES['imagefile']['name'], '.'); // Get The File Extention In The Format Of , For Instance, .jpg, .gif or .php

$image_name=time().$file_ext;
$image="images/".$image_name;
$thumb="thumbs/".$image_name;

if (move_uploaded_file($_FILES['imagefile']['tmp_name'], $image))
{
$newimage = image_resize($image, 250, 250);
rename($newimage['file'], $thumb);
}
?>

greens85
12-06-2010, 03:57 PM
Hi Matt,

I actually forgot I posted here and managed to solve it in the meantime... however I doubt that my version is as efficient as yours so I will take a look at your solution!

Once again many thanks for the great function! I have been tearing my hair out trying to find a way of sorting this with the best possible image quality and your solution has been the best by a long way...

many many thanks,

greens85



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum