Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert uploaded images always into JPG format? [closed]

Tags:

php

I have a simple PHP file upload script:

$path = "uploads/";
$actual_image_name = time().$session_id.".".$ext;
$tmp = $_FILES['photoimg']['tmp_name'];
move_uploaded_file($tmp, $path.$actual_image_name);

How can I save the uploaded image in JPG format?

like image 717
Steven Sacradoor Avatar asked Jan 27 '13 17:01

Steven Sacradoor


People also ask

Why can't I save pictures as JPEG?

There are a number of reasons why you might not be able to save a JPEG in Photoshop. The most common reason is that the file format is not supported. If you're using an older version of Photoshop, it may not support the JPEG file format. Another possibility is that the file has been corrupted or damaged.

How do I change the default image file?

First, open File Explorer and right-click on the folder you want to change the default picture. And choose Properties from the context menu. Then click the Customize tab and click the “Choose File” button. For example, here I am changing the default photo for the folder named “Saved pictures.”

What happens to JPEG images when you open and edit them frequently?

JPEGs Lose Quality Every Time They're Opened: False Simply opening or displaying a JPEG image doesn't harm it in any way. Saving an image repeatedly during the same editing session without ever closing the image will not accumulate a loss in quality.


2 Answers

Try this:

<?php

$path = "uploads/";

$img = $_FILES['photoimg']['tmp_name'];
$dst = $path . $_FILES['photoimg']['name'];

if (($img_info = getimagesize($img)) === FALSE)
  die("Image not found or not an image");

$width = $img_info[0];
$height = $img_info[1];

switch ($img_info[2]) {
  case IMAGETYPE_GIF  : $src = imagecreatefromgif($img);  break;
  case IMAGETYPE_JPEG : $src = imagecreatefromjpeg($img); break;
  case IMAGETYPE_PNG  : $src = imagecreatefrompng($img);  break;
  default : die("Unknown filetype");
}

$tmp = imagecreatetruecolor($width, $height);
imagecopyresampled($tmp, $src, 0, 0, 0, 0, $width, $height, $width, $height);
imagejpeg($tmp, $dst.".jpg");

?>
like image 200
markus Avatar answered Sep 20 '22 18:09

markus


Make sure GD Library is installed and use it to convert gif or png to jpg like

    if ($_FILES["photoimg"]["type"] == "image/jpeg" || $_FILES["photoimg"]["type"] == "image/png" || $_FILES["photoimg"]["type"] == "image/gif" )
    {
      if ($_FILES["photoimg"]["type"] == "image/png" || $_FILES["photoimg"]["type"] == "image/gif" )
      {
       imagejpeg(imagecreatefromstring(file_get_contents($_FILES["photoimg"]["tmp_name"])), "converted.jpg");
       $actual_image_name = time().$session_id.".".$ext;
       move_uploaded_file("converted.jpg", $path.$actual_image_name);
      }
      else
      {
        $actual_image_name = time().$session_id.".".$ext;
        $tmp = $_FILES['photoimg']['tmp_name'];
        move_uploaded_file($tmp, $path.$actual_image_name);
      }
    }
like image 34
Deadlock Avatar answered Sep 20 '22 18:09

Deadlock