【问题标题】:Resize image on server在服务器上调整图像大小
【发布时间】:2015-09-05 02:41:05
【问题描述】:

我制作了一个负责上传图像的文件,然后将这些图像移动到服务器中的文件夹中。我想我不能直接在 $_FILES 数组中调整图像大小,所以我认为我必须在服务器中调整图像大小,所以我的问题是,如何调整服务器中的图像大小?

这是我拥有的代码的一部分:

//This is after getting target which is the file saved on the server

move_uploaded_file($_FILES[$str]['tmp_name'], $target);

scale_image($target);

现在函数 scale_image()

function scale_image($image)
{

    if(!empty($image)) //the image to be uploaded is a JPG I already checked this
    {
        $source_image = imagecreatefromjpeg($image);
        $source_imagex = imagesx($source_image);
        $source_imagey = imagesy($source_image);

        $dest_imagex = 300;
        $dest_imagey = 200;

        $image = imagecreatetruecolor($dest_imagex, $dest_imagey);
        imagecopyresampled($image, $source_image, 0, 0, 0, 0,
        $dest_imagex, $dest_imagey, $source_imagex, $source_imagey);

    }
}

但这似乎不起作用,它移动了文件但没有调整大小。

【问题讨论】:

标签: php image-resizing


【解决方案1】:

PHP 内置了GD library

有许多功能可用于处理图像,但无需重新发明轮子。

查看这个要点,了解一个简单的图像处理类 - https://gist.github.com/880506

这是一个示例用法...

$im = new ImageManipulator($_FILES['field_name']['tmp_name']);
$im->resample(640, 480); // resize to 640x480
$im->save('/path/to/destination/image.jpg', IMAGETYPE_JPEG);

【讨论】:

  • 谢谢你,这真的很有帮助,我把它的一部分放到我自己的代码中
【解决方案2】:

我没有在服务器的目录中创建文件,所以这就是我所做的 move_uploaded_file($_FILES[$str]['tmp_name'], $target); scale_image($target,$target);

现在函数 scale_image()

function scale_image($image,$target)
{
  if(!empty($image)) //the image to be uploaded is a JPG I already checked this
  {
     $source_image = imagecreatefromjpeg($image);
     $source_imagex = imagesx($source_image);
     $source_imagey = imagesy($source_image);

     $dest_imagex = 300;
     $dest_imagey = 200;

     $image2 = imagecreatetruecolor($dest_imagex, $dest_imagey);
     imagecopyresampled($image2, $source_image, 0, 0, 0, 0,
     $dest_imagex, $dest_imagey, $source_imagex, $source_imagey);

     imagejpeg($image2, $target, 100);

  }
}

非常感谢大家,你们给我的资源帮助我创建了这个函数。

【讨论】:

    【解决方案3】:

    将上传的文件移动到 tmp 目录(使用 $_FILES 中的 tmp_name 作为原始位置),使用 gd 将其读入,调整大小然后保存到最终目录。

    http://php.net/manual/en/function.move-uploaded-file.php http://us3.php.net/manual/en/function.imagecreate.php http://us3.php.net/manual/en/function.imagecopyresized.php

    【讨论】:

    • 我做了类似的东西,但我无法将调整大小的图像复制到服务器。
    猜你喜欢
    • 1970-01-01
    • 2017-08-19
    • 2015-06-09
    • 2017-07-09
    • 2013-02-01
    • 2016-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多