首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >上传图片:转换格式

上传图片:转换格式
EN

Stack Overflow用户
提问于 2013-02-10 21:42:16
回答 2查看 149关注 0票数 0

我想转换一个上传的图像为其原始格式,(jpg,jpeg rpng等)但我需要重新调整这些图像的大小为150px宽和210px高。是否可以在复制时更改大小,或者必须转换大小?

这是不成功的:

代码语言:javascript
复制
    $uploaddir1 = "/home/myweb/public_html/temp/sfds454.png";
    $uploaddir2 = "/home/myweb/public_html/images/sfds454.png";

    $cmd = "/usr/bin/ffmpeg -i $uploaddir1 -vframes 1 -s 150x210 -r 1 -f mjpeg $uploaddir2";
    @exec($cmd);
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-02-11 09:12:17

我最近不得不解决这个问题,并实现了这个简单的缓存解决方案:

代码语言:javascript
复制
<?php
function send($name, $ext) {
    $fp = fopen($name, 'rb');
    // send the right headers
    header("Content-Type: image/$ext");
    header("Content-Length: " . filesize($name));

    // dump the picture and stop the script
    fpassthru($fp);
    exit;
}

error_reporting(E_ALL);
ini_set('display_errors', 'On');

if (isset($_REQUEST['fp'])) {
    $ext = pathinfo($_REQUEST['fp'], PATHINFO_EXTENSION);

    $allowedExt = array('png', 'jpg', 'jpeg');
    if (!in_array($ext, $allowedExt)) {
        echo 'fail';
    }

    if (!isset($_REQUEST['w']) && !isset($_REQUEST['h'])) {
        send($_REQUEST['fp']);
    }
    else {
        $w = $_REQUEST['w'];
        $h = $_REQUEST['h'];

        //use height, width, modification time and path to generate a hash
        //that will become the file name
        $filePath = realpath($_REQUEST['fp']);
        $cachePath = md5($filePath.filemtime($filePath).$h.$w);
        if (!file_exists("tmp/$cachePath")) {
            exec("gm convert -quality 80% -colorspace RGB -resize " .$w .'x' . $h . " $filePath tmp/$cachePath");
        }
        send("tmp/$cachePath", $ext);

    }
}
?>

我注意到了一些事情:

  1. Graphicsmagick的转换速度比imagemagick快得多,虽然我没有使用最终产品cuda processing.
  2. For测试转换,但我使用该语言的本地图形库在ASP中重新实现了这段代码。这又快了很多,但如果出现内存不足错误,就会崩溃(在我的工作站上工作得很好,但在4 4GB内存的服务器上就不行了)。
票数 1
EN

Stack Overflow用户

发布于 2013-02-10 21:46:35

你可以用gd代替ffmpeg。要转换图像或调整图像大小,请参阅此示例:http://ryanfait.com/resources/php-image-resize/resize.txt

用于gd的Php库

http://php.net/manual/en/function.imagecopyresampled.php

在该页面中还有一些调整脚本大小的示例。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14798351

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档