【发布时间】:2015-10-24 01:04:30
【问题描述】:
所以我有一个网站,允许用户通过上传图片来更改网站上的个人资料图片。我已经整理好文件名的上传和更改。但我需要将所有上传的图像转换为 jpg 格式。这可能吗,我尝试查看其他一些示例,但我无法理解它们。如果有人可以为此提供一些附加代码或任何其他帮助,我将不胜感激。
注意:我今年 14 岁,只有 php 的基本知识。
再次感谢您。
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.";
}
}
?>
【问题讨论】:
-
你知道你的php是用imagik设置的吗? php.net/manual/en/imagick.setimageformat.php
-
@ParrisVarney 我认为我的服务提供商(fastshosts)没有这个
-
@Unni Babu 我已经看过这段代码,但我不知道如何在我的代码中实现。?
-
看起来fasthosts可能有它,我们这是你的计划吗? buy-hosting.net/hosting-plan-details/fasthosts.html