【问题标题】:Gd Library Flip image and saving itGd 库翻转图像并保存
【发布时间】:2014-06-25 11:22:42
【问题描述】:

我正在使用 PHP GD 库来翻转图像,然后保存翻转的图像。但是我能够成功翻转图像,但我不知道如何将其以另一个名称保存在文件夹中。我的代码是

$filename = '324234234234.jpg';
header('Content-type: image/jpeg');
$im = imagecreatefromjpeg($filename);
imageflip($im, IMG_FLIP_VERTICAL);
imagejpeg($im);

【问题讨论】:

    标签: php jquery gd


    【解决方案1】:

    您需要将第二个参数传递给 imagejpeg($im) 调用,其中包含您要存储的文件的路径。

    imagejpeg($im, 'path/to/new/file.jpg');
    

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

    【讨论】:

    • 我用过这个,但它显示错误“图像无法显示,因为它包含错误”
    • 还有原始图像不创建需要帮助
    • 它应该可以工作,加载页面时图像将不可见,因为如果您设置路径,它将不会显示图像,而是将其存储在您的服务器中。
    【解决方案2】:

    查看imagejpeg 的文档,其中解释了第二个参数与$filename 相关:

    bool imagejpeg ( resource $image [, string $filename [, int $quality ]] )

    保存文件的路径。如果不设置或NULL,则直接输出原始图像流。

    所以只需像这样添加新文件名作为第二个参数;假设新名称的名称是new_filename.jpg。还要注释掉或完全删除 header 行,这样您就不会将图像输出到浏览器,而是将其保存到文件中,这样就不需要 header

    $filename = '324234234234.jpg';
    // header('Content-type: image/jpeg');
    $im = imagecreatefromjpeg($filename);
    imageflip($im, IMG_FLIP_VERTICAL);
    imagejpeg($im, 'new_filename.jpg');
    

    另外请注意,虽然imageflip 很好用,但它仅在 PHP 5.5 及更高版本中可用;它在 PHP 5.4 或更低版本中不可用:

    (PHP 5 >= 5.5.0)

    图像翻转 — 使用给定模式翻转图像

    所以如果你想在 PHP 5.4 或更低版本中使用它,你需要在代码中创建逻辑来自行翻转它或使用a function like this。它只能水平翻转,因此您必须将其调整为垂直翻转,但在此处发布,以便您在需要时可以探索和使用:

    /**
     * Flip (mirror) an image left to right.
     *
     * @param image  resource
     * @param x      int
     * @param y      int
     * @param width  int
     * @param height int
     * @return bool
     * @require PHP 3.0.7 (function_exists), GD1
     */
    function imageflip(&$image, $x = 0, $y = 0, $width = null, $height = null)
    {
        if ($width  < 1) $width  = imagesx($image);
        if ($height < 1) $height = imagesy($image);
        // Truecolor provides better results, if possible.
        if (function_exists('imageistruecolor') && imageistruecolor($image))
        {
            $tmp = imagecreatetruecolor(1, $height);
        }
        else
        {
            $tmp = imagecreate(1, $height);
        }
        $x2 = $x + $width - 1;
        for ($i = (int) floor(($width - 1) / 2); $i >= 0; $i--)
        {
            // Backup right stripe.
            imagecopy($tmp,   $image, 0,        0,  $x2 - $i, $y, 1, $height);
            // Copy left stripe to the right.
            imagecopy($image, $image, $x2 - $i, $y, $x + $i,  $y, 1, $height);
            // Copy backuped right stripe to the left.
            imagecopy($image, $tmp,   $x + $i,  $y, 0,        0,  1, $height);
        }
        imagedestroy($tmp);
        return true;
    }
    

    【讨论】:

    • 我试了一下,但它不起作用,原始图像也无法创建,需要更多帮助
    • 您确定图像没有创建或保存吗?我认为您需要推荐删除 header 行。查看我的编辑。
    • 是的,我完全确定它既不创建也不保存
    • @user3607861 提供的代码应该可以工作。唯一剩下的问题是 Web 服务器是否有权将文件写入与324234234234.jpg 相同的区域?检查文件权限并确保同一用户拥有写入文件的目录。如果您没有收到错误,则应保存该文件。
    • 在评论标题行之后,它也无法进行翻转工作,因为链接显示beta.technology-architects.com/javaid,但我需要保存这个翻转的图像
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-30
    相关资源
    最近更新 更多