【问题标题】:PHP Image resize with all typesPHP图像调整所有类型的大小
【发布时间】: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');

?>

【问题讨论】:

    标签: php image upload resize


    【解决方案1】:

    查看处理图像的各种功能:

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

    【讨论】:

      【解决方案2】:

      您有可用的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选对了。

      【讨论】:

        猜你喜欢
        • 2011-03-31
        • 2011-11-25
        • 2012-01-09
        • 1970-01-01
        • 2011-02-18
        • 2011-08-30
        • 2010-09-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多