Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Gd库翻转图像并保存它_Php_Jquery_Gd - Fatal编程技术网

Php Gd库翻转图像并保存它

Php Gd库翻转图像并保存它,php,jquery,gd,Php,Jquery,Gd,我正在使用PHP GD库翻转一个图像,然后保存翻转后的图像。然而,我能够成功地翻转图像,但我不知道如何将它以另一个名称保存在文件夹中。我的代码是 $filename = '324234234234.jpg'; header('Content-type: image/jpeg'); $im = imagecreatefromjpeg($filename); imageflip($im, IMG_FLIP_VERTICAL); imagejpeg($im); 您需要向imagejpeg($im)调

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

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

您需要向imagejpeg($im)调用传递第二个参数,该参数包含要存储的文件的路径

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

查看文档,了解第二个参数与
$filename
的关系:

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

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

因此,只需添加新文件名作为第二个参数,如下所示;假设新名称的名称为
new\u filename.jpg
。同时注释掉或完全删除
标题
行,这样您就不会将图像输出到浏览器,而是将其保存到文件中,这样就不需要
标题

$filename = '324234234234.jpg';
// header('Content-type: image/jpeg');
$im = imagecreatefromjpeg($filename);
imageflip($im, IMG_FLIP_VERTICAL);
imagejpeg($im, 'new_filename.jpg');
还请注意,虽然使用起来很好,但它仅在PHP5.5及更高版本中可用;它在PHP 5.4或更低版本中不可用:

(PHP 5>=5.5.0)

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

因此,如果您想在PHP5.4或更低版本中使用它,您需要创建逻辑,在代码或使用中自己翻转它。它只会水平翻转,因此您必须将其调整为垂直翻转,但在此处发布,以便您可以在需要时进行探索和处理:

/**
 * 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;
}
/**
*从左向右翻转(镜像)图像。
*
*@param图像资源
*@param x int
*@param y int
*@param width int
*@param高度int
*@returnbool
*@require PHP 3.0.7(函数_存在),GD1
*/
函数imageflip(&$image,$x=0,$y=0,$width=null,$height=null)
{
如果($width<1)$width=imagesx($image);
如果($height<1)$height=imagesy($image);
//如果可能,Truecolor可以提供更好的结果。
if(函数_存在('imagestruecolor')&&imagestruecolor($image))
{
$tmp=ImageCreateTureColor(1,$height);
}
其他的
{
$tmp=imagecreate(1,$height);
}
$x2=$x+$width-1;
对于($i=(int)楼层($width-1)/2);$i>=0;$i--)
{
//备份右条纹。
imagecopy($tmp,$image,0,0,$x2-$i,$y,1,$height);
//将左条纹复制到右条纹。
imagecopy($image,$image,$x2-$i,$y,$x+$i,$y,1,$height);
//将备份的右条纹复制到左侧。
imagecopy($image、$tmp、$x+$i、$y、0、0、1、$height);
}
图像处理(tmp);
返回true;
}

我使用了此选项,但它显示了错误“图像无法显示,因为它包含错误”以及原始图像不创建需要帮助请它应该可以工作,加载页面时,图像将不可见,因为如果设置路径,它将不显示图像,而是将其存储在服务器中。我尝试过,但不起作用,而且原始图像也无法创建。需要更多帮助请确认图像未创建或保存吗?我认为您需要删除
标题
行。查看我的编辑。是的,我完全确定它既不创建也不创建saved@user3607861所呈现的代码应该可以工作。剩下的唯一问题是web服务器是否有权将文件写入与
324234.jpg
相同的区域?检查文件权限并确保同一用户拥有写入文件的目录。如果没有错误,则应保存该文件。在注释标题行后,该文件也不会像链接显示的那样进行翻页操作,但我需要保存此翻页图像