Uploading and resizing images

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ianao
Forum Newbie
Posts: 12
Joined: Fri May 22, 2009 7:03 am

Uploading and resizing images

Post 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
Last edited by Benjamin on Mon Jun 08, 2009 10:38 am, edited 1 time in total.
Reason: Added [code=php] tags.
socket1
Forum Commoner
Posts: 82
Joined: Mon Dec 08, 2008 7:40 pm
Location: Shokan, New York

Re: Uploading and resizing images

Post 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.
ianao
Forum Newbie
Posts: 12
Joined: Fri May 22, 2009 7:03 am

Re: Uploading and resizing images

Post 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 :|
socket1
Forum Commoner
Posts: 82
Joined: Mon Dec 08, 2008 7:40 pm
Location: Shokan, New York

Re: Uploading and resizing images

Post 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.
ianao
Forum Newbie
Posts: 12
Joined: Fri May 22, 2009 7:03 am

Re: Uploading and resizing images

Post 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
socket1
Forum Commoner
Posts: 82
Joined: Mon Dec 08, 2008 7:40 pm
Location: Shokan, New York

Re: Uploading and resizing images

Post 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.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Uploading and resizing images

Post 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.
socket1
Forum Commoner
Posts: 82
Joined: Mon Dec 08, 2008 7:40 pm
Location: Shokan, New York

Re: Uploading and resizing images

Post by socket1 »

I haven't done any formal tests. But I like ImageMagick better since it supports more formats and requires less code.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Uploading and resizing images

Post 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.
socket1
Forum Commoner
Posts: 82
Joined: Mon Dec 08, 2008 7:40 pm
Location: Shokan, New York

Re: Uploading and resizing images

Post 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.
Post Reply