php删除图像周围的像素边框

时间:2015-03-13 09:35:26

标签: php image imagemagick gd

我使用以下代码显示远程图像并缓存它的本地版本。

现在我需要找到一种方法来删除图像周围的10个像素,因为在显示/缓存图像之前需要删除边框。

如何使用php删除图像顶部,底部,右侧和左侧的10个像素?

header('Content-type: image/png');

$path = ".......";
$CACHE_FILE_PATH = "images_tshirts/mini_t/".$a.".png";

if(file_exists($CACHE_FILE_PATH)) {
    echo @file_get_contents($CACHE_FILE_PATH);
} 
else {
    $image = imagecreatefromstring(file_get_contents($path));
    // Send the image
    imagepng($image, $CACHE_FILE_PATH);
    echo @file_get_contents($CACHE_FILE_PATH);
    exit();
}
?>

1 个答案:

答案 0 :(得分:1)

  

imagecrop - 使用给定的坐标和大小x,y裁剪图像,   宽度和高度

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

编辑:来自链接页面的示例,如请求的那样。修改为占用10px边框:



<?php
// Create a blank image and add some text
$ini_filename = 'test.JPG';
$im = imagecreatefromjpeg($ini_filename );

$ini_x_size = getimagesize($ini_filename )[0];
$ini_y_size = getimagesize($ini_filename )[1];

//the minimum of xlength and ylength to crop.
$crop_measure = min($ini_x_size, $ini_y_size);

// Set the content type header - in this case image/jpeg
//header('Content-Type: image/jpeg');

$to_crop_array = array('x' =>0 , 'y' => 0, 'width' => $crop_measure, 'height'=> $crop_measure);
$thumb_im = imagecrop($im, $to_crop_array);

imagejpeg($thumb_im, 'thumb.jpeg', 100);
?>
&#13;
&#13;
&#13;