我见过很多例子,但我不确定它如何与我目前拥有的上传脚本相关联。有人可以解释我如何能够使用ImageMagick或其他类似的东西将上传的图像调整为固定宽度,同时保持w:h比率?
我用Google搜索并查看了超过150个网页,但它们都没有真正给出一个我觉得适用于我的情况的例子。任何帮助,将不胜感激。这是我的代码:
// Include Extension finder function.
include_once 'find_extension.php';
// Function Gathers the Input Name, Store Number and Picture Number. The picture number is assigned by the loop that will be cycling through the images to be uploaded. The store Number and the Picure Number will make the file name.
function uploadImage($inputname, $storenum, $picturenum){
// A few parameters, to restrict file types and file size to 20kb.
if ((($_FILES[$inputname]["type"] == "image/gif")
|| ($_FILES[$inputname]["type"] == "image/png")
|| ($_FILES[$inputname]["type"] == "image/jpeg")
|| ($_FILES[$inputname]["type"] == "image/pjpeg"))
&& ($_FILES[$inputname]["size"] < 200000))
{
// If there is an error, echo the error, if not continue.
if ($_FILES[$inputname]["error"] > 0){
echo "Error: " . $_FILES[$inputname]["error"] . "<br />";
}else{
// Echo out File information.
/* echo "Upload: " . $_FILES[$inputname]["name"] . "<br />";
echo "Type: " . $_FILES[$inputname]["type"] . "<br />";
echo "Size: " . ($_FILES[$inputname]["size"] / 1024) . " Kb<br />";
echo "Stored in: " . $_FILES[$inputname]["tmp_name"]; */
}
// Get the extension so we can rename using the previous function.
$ext = findexts($_FILES[$inputname]["name"]);
$newpicturename = $storenum . $picturenum . "." . $ext;
// Check to see if the file exists, if it does then tell the user. If not continue.
if (file_exists("userimages/" . $newpicturename)){
echo "<br>" . $newpicturename . " already exists. ";
}else{
// Uploads the file.
move_uploaded_file($_FILES[$inputname]["tmp_name"], "userimages/" . $newpicturename);
$picturefield = "picture" . $picturenum;
$picturepath = "userimages/" . $newpicturename;
//Inserts data into ad DB
mysql_query("UPDATE storead SET $picturefield='$picturepath' WHERE storenum='$storenum'");
// Tells the User.
// echo "Stored in: " . "userimages/" . $newpicturename;
}
}else{
// If the upload does not meet the parameters above, then tell the user and cancel.
echo "Error uploading " . $_FILES[$inputname]["name"] . ". File may be too large or of unnacepted format.";
}
}
谢谢:)
答案 0 :(得分:3)
答案 1 :(得分:1)
list($originalWidth, $originalHeight) = getimagesize($originalFile);
$width = 250; // whatever your fixed width is
$height = ceil(($width / $originalWidth) * $originalHeight);
然后按照鲍勃的指示
答案 2 :(得分:1)
// Get the image data.
// This REALLY needs some form of security, doing it like in this example is *bad*.
// There are other functions than "imagecreatefromjpeg" to handle other formats.
$image = imagecreatefromjpeg($_FILES["userfile"]["tmp_name"]);
// Get image dimensions.
$imgX = imagesx($image);
$imgY = imagesy($image);
// Get aspect ratio.
// Biggest dimension is what we will use to constrain the thumbnail.
if($imgX > $imgY) {
$multiplier = 150 / $imgX;
} else {
$multiplier = 150 / $imgY;
}
// Create a scaled down version of the uploaded image.
$thumb = imagecreatetruecolor($imgX * $multiplier, $imgY * $multiplier);
imagecopyresampled($thumb, $image, 0,0,0,0, $imgX * $multiplier, $imgY * $multiplier, $imgX, $imgY);
评论很多!这大致是我用来在我自己的画廊中创建缩略图的代码。 我能看到的唯一问题是,如果您需要缩略图在其最大尺寸中不是正方形,则此代码仅限制为正方形。