2

I have looked around for information about this but haven't been able to find what I am looking for.

I have a site where users are able to upload an image for display on their user profile.

However, what I want to do is be able to make every photo that is uploaded a jpg. I feel like forcing people to only upload JPG files isn't good, but if I am able to make all files JPG, then that would be great.

Here is my code for when a user uploads an image:

require('includes/config.php'); 

// Upload and Rename File

if (isset($_POST['submit']))
{
    $filename = $_FILES["file"]["name"];
    $file_basename = substr($filename, 0, strripos($filename, '.')); // get file extention
    $file_ext = substr($filename, strripos($filename, '.')); // get file name
    $filesize = $_FILES["file"]["size"];
    $allowed_file_types = array('.jpg','.jpeg','.png','.gif');  

if (in_array($file_ext,$allowed_file_types) && ($filesize < 2000000))
{   
    // Rename file
    $newfilename = ($_SESSION['memberID']) . $file_ext;
    if (file_exists("upload/" . $newfilename))
    {
        // file already exists error
        echo "You have already uploaded this file.";
    }
    else
    {       
        move_uploaded_file($_FILES["file"]["tmp_name"], "uploads/" . $newfilename);
        echo "File uploaded successfully.";     
    }
}
elseif (empty($file_basename))
{   
    // file selection error
    echo "Please select a file to upload.";
} 
elseif ($filesize > 200000)
{   
    // file size error
    echo "The file you are trying to upload is too large.";
}
else
{
    // file type error
    echo "Only these file typs are allowed for upload: " . implode(', ',$allowed_file_types);
    unlink($_FILES["file"]["tmp_name"]);
}

If anyone is able to suggest a way of changing uploaded files to JPG then please let me know.

  • To force all uploaded images to be JPG you should edit this line `$allowed_file_types = array('.jpg','.jpeg','.png','.gif');`. If you are interested in reformatting all uploads as JPG you should edit your question to be crystal clear. – Stefan Crain Apr 06 '18 at 02:25
  • I tried to make that clear in the 3 paragraph. Apologies. I have changed the last line. – Lewis Pickles Apr 06 '18 at 02:31
  • Possible duplicate of [How can I convert all images to jpg?](https://stackoverflow.com/questions/14549446/how-can-i-convert-all-images-to-jpg) – Stefan Crain Apr 06 '18 at 02:35

1 Answers1

0

Try this code: originalImage is the path of... the original image... outputImage is self explaining enough. Quality is a number from 0 to 100 setting the output jpg quality (0 - worst, 100 - best)

function convertImage($originalImage, $outputImage, $quality)
{
    // jpg, png, gif or bmp?
    $exploded = explode('.',$originalImage);
    $ext = $exploded[count($exploded) - 1]; 

    if (preg_match('/jpg|jpeg/i',$ext))
        $imageTmp=imagecreatefromjpeg($originalImage);
    else if (preg_match('/png/i',$ext))
        $imageTmp=imagecreatefrompng($originalImage);
    else if (preg_match('/gif/i',$ext))
        $imageTmp=imagecreatefromgif($originalImage);
    else if (preg_match('/bmp/i',$ext))
        $imageTmp=imagecreatefrombmp($originalImage);
    else
        return 0;

    // quality is a value from 0 (worst) to 100 (best)
    imagejpeg($imageTmp, $outputImage, $quality);
    imagedestroy($imageTmp);

    return 1;
}
software is fun
  • 7,286
  • 18
  • 71
  • 129
  • If I call this function instead of the move_uploaded_file, then how would I tell it where to put the file? (Assuming that is what I do with it). – Lewis Pickles Apr 06 '18 at 02:39
  • Once you have outputImage, it’s a JPG and you can move the file to anywhere you wish. If the file is too large or not the right dimensions, you can display an error/warning. – software is fun Apr 06 '18 at 02:47