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


php remove pixels border around an image

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

现在我需要找到一种方法来删除图像周围的 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();
}
?>

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