2

I have a website where I allow users to upload photos of any type.

I.e gif, jpeg, png

Now, how can I convert the files on upload to JPEG, if they aren't already?

Thanks

Here is my PHP:

if ($_FILES['media']['size'] != 0) {
    $target= UPLOADPATH . $media;
    move_uploaded_file($_FILES['media']['tmp_name'], $target);
    $query= "INSERT INTO posts (user_id, story, media, date, view, type) VALUES    
            ('$user_id', '$story', '$media', now(), '$view', '1')";
    mysqli_query($connect, $query)
        or die('error with query 2');
}
Matt
  • 163
  • 3
  • 10
  • 1
    possible duplicate of [Use PHP to convert PNG to JPG with compression?](http://stackoverflow.com/questions/1201798/use-php-to-convert-png-to-jpg-with-compression) – Quentin Jan 07 '13 at 17:13
  • The answer was already given in http://stackoverflow.com/questions/755781/convert-jpg-image-to-gif-png-bmp-format-using-php – Mihai8 Jan 07 '13 at 17:14
  • Why do you want to convert them to jpg? PNG and GIF are both perfectly valid formats? – Toby Allen Jan 07 '13 at 17:20

3 Answers3

1

Simply renaming their extensions will not do the trick, you'll need to use an image processor to alter the file.

Check out imagemagick

Kristian
  • 21,204
  • 19
  • 101
  • 176
1

In PHP you start off by reading the content of the uploaded files into imagecreatefromstring() with something like file_get_content($filePath). From there you use one imagejpeg to write to another file with extension .jpg

Here is a sample:

$handle = imagecreatefromstring(file_get_content($filePath));
imagejpeg($handle,'newfile.jpg',100);

Reference

  1. imagejpeg
  2. imagecreatefromstring
  3. file_get_content
shawndreck
  • 2,039
  • 1
  • 24
  • 30
0

maybe try functions like imagecreatefrom*()

imagecreatefrompng(youimage);

imagecreatefromgif(youimage);

before use this functions you need to know the file format.

Then use imagejpeg() function

KryDos
  • 447
  • 4
  • 14