【问题标题】:php remove pixels border around an imagephp删除图像周围的像素边框
【发布时间】:2015-05-15 17:42:32
【问题描述】:

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

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

【问题讨论】:

标签: php image imagemagick gd


【解决方案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);
?>

【讨论】:

  • 已添加,您只需向下滚动该页面
  • 我还是不明白怎么去掉 10pixels。
  • imagecrop($img, $rect) 其中 $rect 在您的情况下是一个数组(代表裁剪矩形),具有以下键:'x' = 10, 'y' = 10, 'width' = $ img_width - 10, '高度' = $img_height - 10
猜你喜欢
  • 2011-08-22
  • 1970-01-01
  • 1970-01-01
  • 2017-10-28
  • 1970-01-01
  • 2011-06-08
  • 2023-04-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多