[英]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. 但是我需要将所有上传的图像转换为jpg格式。 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. 注意:我今年14岁,只具备php的基本知识。
Thank you again. 再次感谢你。
PHP CODE "upload.php" (opened after form is submitted with image) PHP CODE “ upload.php”(在提交带有图像的表单后打开)
<?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. Image Magic应该安装在服务器上。 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() 您的情况下,在move_uploaded_file()之后使用它
$cmd = "convert $newFileName ".$target_dir."YourfileName".".jpg' ;
exec($cmd);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.