简体   繁体   English

在PHP中保存裁剪图像

[英]Save Crop image in php

I crop image using imagecopy in php. 我在php中使用imagecopy裁剪图像。 but I want save it to my old path. 但我想把它保存到我原来的路上。 what can I used for it ? 我能用它做什么? this is my method how i crop the image . 这是我的方法如何裁剪图像。

$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);

I used this method, but it was not work for me. 我使用这种方法,但它对我不起作用。

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

Just specify where you want it to save the file 只需指定保存文件的位置即可

imagejpeg($crop,$FILENAME);

EDIT 编辑

Based on your comment below, the problem is not saving the file , it is that you do not have a valid image resource to begin with. 根据您在下面的评论,问题不是保存文件,而是您没有开始使用的有效图像资源。

$image = imagecreatefromjpeg($filename);

should be 应该

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

You are telling it to copy $x,$y to 250,300 from the orignal image into the new image, but there has been no check to ensure that you have an image that is big enough to do that, or indeed that x and y exist or that they are less than 250,300 你告诉它将$ x,$ y从原始图像复制到250,300到新图像中,但是没有检查以确保你有一个足够大的图像,或者确实存在x和y或者他们不到250,300

Also you care currently trying to imagedestroy() a filename. 你还关心当前尝试imagedestroy()文件名。 You can only imagedestroy an image resource. 您只能对图像资源进行图像处理。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM