简体   繁体   中英

PHP Image upload which then converts into jpg format

So I have a website that allows users to change there profile picture on the site by uploading an image. I have the uploading and changing of file name sorted out. But I need all the images uploaded to be converted into a jpg formate. Is this possible, I tried looking at some other examples but I couldn't understand them. If anyone could provide some add on code to this or any other kind of help I would be very grateful.

Note: I am a 14 year old and only have basic knowledge with php.

Thank you again.

PHP CODE "upload.php" (opened after form is submitted with image)

<?php
    $target_dir = "uploads/";
    $newFileName = $target_dir .'YourfileName'.'.'. pathinfo($_FILES["fileToUpload"]["name"] ,PATHINFO_EXTENSION); //get the file extension and append it to the new file name
    $uploadOk = 1;
    $imageFileType = pathinfo($_FILES["fileToUpload"]["name"] ,PATHINFO_EXTENSION);
    // Check if image file is a actual image or fake image
    if(isset($_POST["submit"])) {
        $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
        if($check !== false) {
            echo "File is an image - " . $check["mime"] . ".";
            $uploadOk = 1;
        } else {
            echo "File is not an image.";
            $uploadOk = 0;
        }
    }
    // Check if file already exists
    if (file_exists($target_file)) {
        echo "Sorry, file already exists.";
        $uploadOk = 0;
    }
    // Check file size
    if ($_FILES["fileToUpload"]["size"] > 500000) {
        echo "Sorry, your file is too large.";
        $uploadOk = 0;
    }
    // Allow certain file formats
    if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
    && $imageFileType != "gif" ) {
        echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
        $uploadOk = 0;
    }
    // Check if $uploadOk is set to 0 by an error
    if ($uploadOk == 0) {
        echo "Sorry, your file was not uploaded.";
    // if everything is ok, try to upload file
    } else {
        if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"],  $newFileName)) {
            echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";

        } else {
            echo "Sorry, there was an error uploading your file.";
        }
    }
    ?>

try this code

$f = explode(".",$_FILES["fileToUpload"]["name"]);

$file = $f[0].".jpg";
$newFileName = $target_dir .$file;

Image magic should be installed on the server. For this command

$cmd = "convert path/to/image/imagename.extension  path/to/image/imagename.jpg";
exec($cmd);

In your case, use this after move_uploaded_file()

$cmd = "convert $newFileName ".$target_dir."YourfileName".".jpg' ;
exec($cmd);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM