【问题标题】:How to resize an image programmatically in php? [duplicate]如何在 php 中以编程方式调整图像大小? [复制]
【发布时间】:2014-05-30 21:07:12
【问题描述】:

我遇到了几个解决方案,但所有这些都是基于上传图片。我想要的是它应该从一个文件夹中获取图像,将其调整为特定大小,然后将其保存在另一个文件夹中。

【问题讨论】:

  • 不是要求我们提供代码,而是先谷歌它,然后尝试它,如果它不起作用,然后问我们一个建设性的问题。

标签: php image-processing image-resizing resize-image


【解决方案1】:

我通常为此使用 GD 库。它工作正常。例如,请看这个网址:http://runnable.com/UnF-tFdudNt1AABt/how-to-resize-an-image-using-gd-library-for-php

【讨论】:

    【解决方案2】:

    试试PHP库函数imagecopyresized()

    这是一个示例程序,

    // File and new size
    $filename = 'test.jpg';
    $percent = 0.5;
    
    // Content type
    header('Content-Type: image/jpeg');
    
    // Get new sizes
    list($width, $height) = getimagesize($filename);
    $newwidth = $width * $percent;
    $newheight = $height * $percent;
    
    // Load
    $thumb = imagecreatetruecolor($newwidth, $newheight);
    $source = imagecreatefromjpeg($filename);
    
    // Resize
    imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
    
    // Output
    imagejpeg($thumb);
    

    这是 imagecopyresized() 的手册。 http://www.php.net/manual/en/function.imagecopyresized.php

    【讨论】:

    • 我应该在哪里给出目标路径?甚至在header('Content-Type: image/jpeg')上提示错误;
    • 如果您从手册中复制代码,您至少可以添加对它的引用。
    • @ICanHasCheezburger 抱歉,我现在添加了。
    猜你喜欢
    • 2011-01-12
    • 1970-01-01
    • 1970-01-01
    • 2017-03-23
    • 1970-01-01
    • 2011-06-10
    • 2021-04-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多