Page 1 of 1

Uploading and resizing images

Posted: Mon Jun 08, 2009 4:33 am
by ianao
Hi

I'm developing a site where visitors can upload their own property images. No problem with the upload part, but the problem is with resizing. Firstly, is it a good idea to resize at the server (the alternative being an instruction to the visitor on how to prepare their own images) anyway?

Here is my uploader.php file which I want to resize an image to a maximum of 400px width or height:

Code: Select all

 
<?php
 
    $listingid = $_POST['listing'];
    $filename = $_FILES['file']['name'];
 
    // Set a maximum height and width
    $width = 400;
    $height = 400;
 
    // Content type
    header('Content-type: image/jpeg');
 
    // Get new dimensions
    list($width_orig, $height_orig) = getimagesize($filename);
 
    $ratio_orig = $width_orig/$height_orig;
 
    if ($width/$height > $ratio_orig) {
    $width = $height*$ratio_orig;
    } else {
    $height = $width/$ratio_orig;
    }
 
    // Resample
    $image_p = imagecreatetruecolor($width, $height);
    $image = imagecreatefromjpeg($filename);
    imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
 
    // Output
    imagejpeg($image_p, null, 100);
 
    if($_FILES['file']['name'] !="")
    {
    copy ($_FILES['file']['tmp_name'],
    "/properties/mainimg".$listingid.".jpg")
        or die("Could not copy file");
 
    header("location:get_listings.php");
        exit;
    }
    else{ die("No file specified");}
?> 
 
Any advice would be great.

Many thanks

Re: Uploading and resizing images

Posted: Mon Jun 08, 2009 10:26 am
by socket1
I wouldn't use GD, look into using ImageMagick as it produces better quality resized pictures.
Later I can post in detail a better explanation of how to use image magick but this is part of my code

Code: Select all

exec('/usr/local/bin/convert -quality 85 -resize '.$ResizeWidth.' "path/to/file/to/resize.jpg" "path/to/resized/image.jpg"');
 
Thats obviously if you have a host that runs linux but the same command will work with the windows version of ImageMagick.

Re: Uploading and resizing images

Posted: Tue Jun 09, 2009 2:11 am
by ianao
Hmm. Thanks for the response, but sadly way over my head.

I suppose my question is: should I go down the route of trying to find a way to resize images automatically (I'm concerned about bandwidth problems), or should I insist that users resize their own images before upload?

Best
ianao :|

Re: Uploading and resizing images

Posted: Tue Jun 09, 2009 5:52 am
by socket1
Well I have an image hosting script on my domain and, for thumbnails, I use ImageMagick to resize pictures to a width of 300.

Re: Uploading and resizing images

Posted: Tue Jun 09, 2009 7:04 am
by ianao
I use the GIMP to resize images, but should a user be expected to use a graphics program or should the PHP do it automatically for them?

My client feels it should be done automatically...

best
ianao

Re: Uploading and resizing images

Posted: Tue Jun 09, 2009 10:10 am
by socket1
Well what is the target operating system of the application you are writing?
Linux or Windows?
It shouldn't matter either way but I can help you with a small bit of code to resize the images automatically if I know the OS environment.

Re: Uploading and resizing images

Posted: Tue Jun 09, 2009 10:23 am
by onion2k
socket1 wrote:I wouldn't use GD, look into using ImageMagick as it produces better quality resized pictures.
Interesting. They both use bicubic resampling* so they should be pretty much the same. What makes you think ImageMagick is better? Have you done any tests?

* IM has things like Lanczos3 resampling, which can give a better result than bicubic, but only if you're enlarging the image.

Re: Uploading and resizing images

Posted: Tue Jun 09, 2009 1:54 pm
by socket1
I haven't done any formal tests. But I like ImageMagick better since it supports more formats and requires less code.

Re: Uploading and resizing images

Posted: Fri Jun 12, 2009 3:37 am
by onion2k
socket1 wrote:I haven't done any formal tests. But I like ImageMagick better since it supports more formats and requires less code.
Oh right. That's a bit different to saying ImageMagick is higher quality then. In fact, the opposite might be true. It's fine to say you prefer one over the other, but to make something up in order to support your preference? That's not something we do. I'm actually tempted to ban you for making misleading posts. This forum is for people to learn and improve their PHP knowledge. Writing unsubstantiated rubbish doesn't help.

Re: Uploading and resizing images

Posted: Fri Jun 12, 2009 10:14 am
by socket1
Well I'm sorry if I mislead anybody I was basing that fact on with stock settings without me setting any quality controls ImageMagick had better results, so Yeah I was wrong on that but to go as far to ban me over this? Thats being ridiculous. maybe if I actually made
misleading posts.
then maybe but it was one post.

And I did *not* make that up to support my preference.

Now back to the original post.... I'd be glad to recode the original poster's code to automatically resize images either way but if you're just going to ban me then i'm not even going to come back ... ever.