PHP Image调整所有类型的大小

时间:2014-03-27 01:31:45

标签: php image upload resize

我遵循了一个关于如何使用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');

?>

2 个答案:

答案 0 :(得分:2)

在以下位置查看用于处理图像的各种功能:

http://php.net/manual/en/ref.image.php

答案 1 :(得分:1)

您有following functions可用:

imagecreatefromgd2 - Create a new image from GD2 file or URL
imagecreatefromgd2part - Create a new image from a given part of GD2 file or URL
imagecreatefromgd - Create a new image from GD file or URL
imagecreatefromgif - Create a new image from file or URL
imagecreatefromjpeg - Create a new image from file or URL
imagecreatefrompng - Create a new image from file or URL
imagecreatefromstring - Create a new image from the image stream in the string
imagecreatefromwbmp - Create a new image from file or URL
imagecreatefromwebp - Create a new image from file or URL
imagecreatefromxbm - Create a new image from file or URL
imagecreatefromxpm - Create a new image from file or URL

然后,您可以根据the file extension选择正确的。