Forum Moderators: not2easy

Message Too Old, No Replies

Need opinions on a thought process

on image uploads

         

Drum

7:13 pm on May 1, 2004 (gmt 0)

10+ Year Member



I have a real estate site which allows users to upload photos of their homes. Which do you think is the right way for accept photos:

A. Have script that will automactically reduce the size of the photo pixels if too large and compress the photo if the file is too big; or

B. Only allow images of a certian file size and only allow images that are of a certain pixel size - if the file size or image size are too big then an error massage is sent to the user.

Sanenet

11:24 am on May 3, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Combination of both - try to get the user to upload files correctly, but keep error checking activated.

mifi601

11:43 am on May 3, 2004 (gmt 0)

10+ Year Member



I wanted my clients to be able to upload pics for a long time. Have not done it because of same quandary.

Also I am not sure, how to exactly do it in php.

jbinbpt

12:48 pm on May 3, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I believe that it will be difficult to force clients to only upload a certain image size. I believe that you will need to run your script and adjust the images. Advise the client that this has happened and suggest how they can better prepare their images next time.

On one site I placed a link to Irfanview [irfanview.com] for those clients that needed a freeware image program.

jb

Krapulator

7:02 am on May 5, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It is essential that whatever other methods you use - you must set a maximum file size limit for uploads.

jusdrum

3:21 pm on May 10, 2004 (gmt 0)

10+ Year Member



This is a great time to use GD (or ImageMagik). You can configure GD to resize images for you after someone else uploads them.

As far as which method to choose...

a.) Never require people to think! Let them upload whatever they want and then set-up GD to resize the image for them.

b.) If you have limited funds/experience/time then go ahead and tell the real estate folks to resize and compress their images. My experience tells me this is a very bad idea though.

Here's another thread that dealt with this very issue [webmasterworld.com]

Drum

3:42 pm on May 10, 2004 (gmt 0)

10+ Year Member



jusdrum,

Thanks. I have tried using Coldfusion CFX_Image, however the quality or compression does not work at all so the picture quality comes out really bad.

The hosting service is use does offer the imageMagic PHP component and i am going to try that next. I would love to see the code on how you do it since i am a Coldfusion guy and not a PHP type of guy - is there anyway i can peek at your code for the file upload and resize?

jusdrum

3:49 pm on May 10, 2004 (gmt 0)

10+ Year Member



I didn't set it up, my partner did. I just watched. :-) But, I will ask him if he can send me the code snippet.

jusdrum

3:56 pm on May 10, 2004 (gmt 0)

10+ Year Member



It looks like we used GD, not ImageMagik. Are you still interested in the code?

Drum

4:03 pm on May 10, 2004 (gmt 0)

10+ Year Member



My hosting plan does not have that one - but thanks for the offer.

jusdrum

7:09 pm on May 10, 2004 (gmt 0)

10+ Year Member



In case you, or someone else wants it:

<?php
/*

Resize an image using GD

*** NOTE - REQUIRES GD 2.0 OR HIGHER ***

------------------------------------------------------------------------
$Image is a string path to an image on the disk
$NewWidth is the maximum width you will allow for the image
$NewHeight is the maximum height you will allow for the image (optional)
$DestImage is the filename of the output default is to over-write the old image (optional)
------------------------------------------------------------------------
The function will figure out which dimension already fits in your specifications and size the image accordingly.
The web server needs to have write privileges to the image and/or directory where the output goes.

Returns true on success, false on failure

*** Examples ***

resizeImage("test.jpg",400); // Fit the width to 400 pixels

resizeImage("test.jpg",400,400); // Fit the width to 400 pixels, height to 400 pixels

resizeImage("test.jpg",400,0,"newimage.jpg"); // Fit the width to 400 pixels, height doesn't matter, output to newimage.jpg

*** NOTE - If you specify the last argument (output filename), you must put a height value in (0 if you don't want to do a height). ***

*/

function resizeImage($Image,$NewWidth,$NewHeight = 0,$DestImage = "")
{

// Get the original image info
list ($OrigWidth,$OrigHeight,$OrigType) = getimagesize($Image);

// Figure out how to resize the image
if ($NewHeight)
{
if (($OrigWidth - $NewWidth) >= ($OrigHeight - $NewHeight))
{
$Width = $NewWidth;
$Height = floor(($NewWidth / $OrigWidth) * $OrigHeight);
}
else
{
$Height = $NewHeight;
$Width = floor(($Height / $OrigHeight) * $OrigWidth);
}
}
else
{
$Width = $NewWidth;
$Height = floor(($NewWidth / $OrigWidth) * $OrigHeight);
}

// Create a new blank image based on the image type
if ($OrigType == 1) // GIF
{
$OrigImage = ImageCreateFromGIF($Image);
$NewImage = ImageCreate($Width, $Height);
}
if ($OrigType == 2) // JPG
{
$OrigImage = ImageCreateFromJPEG($Image);
$NewImage = ImageCreateTrueColor($Width, $Height);
}
if ($OrigType == 3) // PNG
{
$OrigImage = ImageCreateFromPNG($Image);
$NewImage = ImageCreateTrueColor($Width, $Height);
}

// Copy the image to the blank image resized
if (ImageCopyResampled($NewImage,$OrigImage, 0, 0, 0, 0, $Width, $Height, $OrigWidth, $OrigHeight))
{
if ($DestImage)
{
$Image = $DestImage;
}
else
{
@unlink($Image);
}
if ($OrigType == 1) // GIF
{
ImageGIF($NewImage,$Image);
}
if ($OrigType == 2) // JPG
{
ImageJPEG($NewImage,$Image,99);
}
if ($OrigType == 3) // PNG
{
ImagePNG($NewImage,$Image);
}
return true;
}
else
{
return false;
}

}

?>