2017-02-24 108 views
-1

我需要使用尺寸来裁剪图像。如何在PHP中用尺寸裁剪图像(没有质量损失)?

并将其保存到JPEG格式的本地。

尺寸,我收到的,

{"left":82.5,"top":48.875,"width":660,"height":371.25} 

我需要从图像的原始大小裁剪。

Ex。图像是1200x800,然后结果图像尺寸从实际尺寸,不调整大小或任何。因为质量应该是一样的。

我怎样才能使用这些参数来裁剪图像?

可能吗?

+0

这些值是什么尺寸? 0.5 px将很难生成 – Martin

回答

0

使用内置imagick class

$image = realpath("/path/to/your/image.extension"); 
$cropped = realpath("/path/to/your/output/image.png"); 

$imObj = new Imagick(); 
$imObj->cropImage($width, $height, $offset_x, $offset_y); 
$imObj->setImageFormat("png"); // this is unnesesary, you can force an image format with the extension of the output filename. 
$imObj->writeImage($cropped); 

至于无损输出,使用具有无损编码的图像格式。 PNG是完美的工作,因为它是专为网络传输而设计的(因此是“Adam-7”隔行扫描)。 检查关于平面设计组无损图像格式此相关的问题:

What are lossless image formats?

+1

Imagick是***没有内置在这»PECL扩展没有与PHP捆绑在一起。 ' – Martin

0

可以使用imageCopyResampled功能,设计非常正是这一点。

$image = imagecreatefromjpeg($imageFileURL); 
/*** 
* resize values (imported) 
***/ 
$left = 82; 
$top = 49; 
$width = 660; 
$height = 371; 

/*** 
* Create destination image 
***/ 
$newImage = imagecreatetruecolor($width,$height); 
$saveToFile = "destintion filespace of image file.jpg" 

if(imagecopyresampled($newImage, $image, //dest/source images 
     0, 0,       // dest coordinates 
    $left, $top,       // source coordinates 
    $width, $height,      // size of area to paste to 
    $width, $height      // size of area to copy from 
)){ 
    imagejpeg($newImage,$saveToFile,100); //zero compression saved to file 
    print "image resized ok!!"; 
} 

新fileimage将与$width$height指定的尺寸和将被从由$left$top给出的值的原始图像的偏移量。从你的问题来看,这看起来像你想要的。这不会调整或更改图像的压缩(直到您保存该文件,然后可能自己设置这些细节)。