【发布时间】:2014-05-05 18:37:27
【问题描述】:
我遵循了一个关于如何使用 PHP 调整图像大小的教程,我认为这比使用几个小时自己尝试做这件事要容易,但我在上传 .jpg 文件以外的任何内容时遇到了问题,这是可以理解的,因为我我正在使用“imagecreatefromjpeg”和“imagejpeg”,但我可以对所有图像类型使用什么其他功能?
这是我的代码:
<?php
$outputDir = "../images/uploads/";
$randomInt = rand(1,1000);
$imgTmpname = $_FILES["myfile"]["tmp_name"];
$newImgName = $randomInt;
$imgSavedName = $randomInt . '.jpg';
move_uploaded_file($imgTmpname,$outputDir.$imgSavedName);
echo $newImgName;
$savedImgDir = $outputDir . $imgSavedName;
$imgSize = getimagesize($savedImgDir);
$imgWidth = $imgSize[0];
$imgHeight = $imgSize[1];
$newSize = ($imgWidth + $imgHeight) / ($imgWidth * ($imgHeight / 45));
$newWidth = $imgWidth * $newSize;
$newHeight = $imgHeight * $newSize;
$newImg = imagecreatetruecolor($newWidth, $newHeight);
$oldImg = imagecreatefromjpeg($savedImgDir);
imagecopyresized($newImg, $oldImg, 0, 0, 0, 0, $newWidth, $newHeight, $imgWidth, $imgHeight);
imagejpeg($newImg, $outputDir.$newImgName.'_thumb'.'.jpg');
?>
【问题讨论】: