加入收藏 | 设为首页 | 会员中心 | 我要投稿 北几岛 (https://www.beijidao.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

在PHP中保存裁剪图像

发布时间:2021-08-28 03:47:30 所属栏目:大数据 来源: https://www.jb51.cc
导读:我在PHP中使用imagecopy裁剪图像.但我想把它保存到我原来的路上.我能用它做什么?这是我的方法如何裁剪图像. $w=250; $h=300; $x=$_POST['x1']; $y=$_POST['y1']; $filename="upload/".$_SESSION['basename']; header('Content-type: image/jpg'); //header(

我在PHP中使用imagecopy裁剪图像.但我想把它保存到我原来的路上.我能用它做什么?这是我的方法如何裁剪图像.

$w=250;
    $h=300;    
    $x=$_POST['x1'];    
    $y=$_POST['y1'];   
    $filename="upload/".$_SESSION['basename'];
    header('Content-type: image/jpg');
    //header('Content-Disposition: attachment; filename='.$src);
    $image = imagecreatefromjpeg($filename);
    $crop = imagecreatetruecolor($w,$h);
    $new = imagecopy ($crop, $image, 0, 0, $x, $y, $w, $h);
    imagejpeg($crop);
    imagedestroy($filename);

我使用这种方法,但它对我不起作用.

$input = 'upload/'.$new;
    $output = 'upload/xxx.jpg';
    file_put_contents($output, file_get_contents($input));

解决方法:

只需指定保存文件的位置即可

imagejpeg($crop,$FILENAME);

编辑

根据您在下面的评论,问题不是保存文件,而是您没有开始使用的有效图像资源.

$image = imagecreatefromjpeg($filename);

应该

$image = imagecreatefromjpeg($filename);
if (!$image)
    exit("not a valid image");

你告诉它将原始图像中的$x,$y复制到250,300到新图像中,但是没有检查以确保你有一个足够大的图像,或者确实存在x和y或者他们不到250,300

你还关心当前尝试imagedestroy()文件名.您只能对图像资源进行图像处理.

imagedestroy($image);
imagedestroy($crop);

(编辑:北几岛)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读