You are here: Re: Upload image works fine unless image is edited first « PHP Programming Language « IT news, forums, messages
Re: Upload image works fine unless image is edited first

Posted by Rik Wasmus on 09/05/07 15:06

On Wed, 05 Sep 2007 16:33:07 +0200, xx75vulcan <xx75vulcan@gmail.com> =

wrote:

> Hi,
>
> I've got a PHP Upload Form that works great, unless that is, the image=

> your uploading has been modified through a photo editing software.
>
> Example: if I upload the image straight from a camera or other, it
> uploads fine. However, If I want to rotate the image, or resize it
> with photoshop or simmilar, making sure to save it again as a jpg, the=

> PHP upload won't upload the image.
>
> It's like editing the image somehow changes it's mime type or
> something?!
> Any ideas?
> Here's my upload script:
>
> <?php
> if($_POST["upload"]) //Check to see if submit was clicked
> {
> // Get the details of "imagefile"
> $filename =3D $_FILES['imagefile']['name'];
> $temporary_name =3D $_FILES['imagefile']['tmp_name'];
> $mimetype =3D $_FILES['imagefile']['type'];

Never trust mimetype. Use something like getimagesize() to determine =

wether it is, and if so get the type of image.

> $filesize =3D $_FILES['imagefile']['size'];
>
> //Open the image using the imagecreatefrom..() command based on the
> MIME type.
> switch($mimetype) {
> case "image/jpg":
> case "image/jpeg":
> case "image/pjpeg":
> $i =3D imagecreatefromjpeg($temporary_name);
> break;
> case "image/gif":
> $i =3D imagecreatefromgif($temporary_name);
> break;
> case "image/png":
> $i =3D imagecreatefrompng($temporary_name);
> break;
=

Add:

default:
echo $mimetype.' is not a supported image';
exit;

> }

I would guess here is where it breaks, due to an unforseen mimetype. Do =
a =

var_dump($i);exit(); here... Probably it isn't set.

> //Delete the uploaded file
> unlink($temporary_name);
>
> //Specify the size of the thumbnail
> $dest_x =3D 350;
> $dest_y =3D 350;
>
> //Is the original bigger than the thumbnail dimensions?
> if (imagesx($i) > $dest_x or imagesy($i) > $dest_y) {
> //Is the width of the original bigger than the height?
> if (imagesx($i) >=3D imagesy($i)) {
> $thumb_x =3D $dest_x;
> $thumb_y =3D imagesy($i)*($dest_x/imagesx($i));
> } else {
> $thumb_x =3D imagesx($i)*($dest_y/imagesy($i));
> $thumb_y =3D $dest_y;
> }
> } else {
> //Using the original dimensions
> $thumb_x =3D imagesx($i);
> $thumb_y =3D imagesy($i);
> }
>
> //Generate a new image at the size of the thumbnail
> $thumb =3D imagecreatetruecolor($thumb_x,$thumb_y);
>
> //Copy the original image data to it using resampling
> imagecopyresampled($thumb, $i ,0, 0, 0, 0, $thumb_x, $thumb_y,
> imagesx($i), imagesy($i));
>
> //Save the thumbnail
> imagejpeg($thumb, "../images/$filename", 80);
>
> //Display FileName
> Print "Value for Image field: $filename";
> $added =3Dtrue;
> }
> ?>


So, have you enabled error_reporting/display_errors? Do you know where t=
he =

code fails? Try to determine exactly where everyting goes wrong.

-- =

Rik Wasmus

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация