2024 年 PHP Conference Japan

影像處理 (ImageMagick)

新增註解

使用者貢獻的註解 3 則註解

mlong-php at mlong dot us
17 年前
以下是如何將已在字串中的圖像(例如,來自資料庫)調整大小、新增邊框並列印出來的範例。我用它來顯示經銷商的標誌

// 從 base64 解碼圖像
$image=base64_decode($imagedata);

// 建立 Imagick 物件
$im = new Imagick();

// 將圖像轉換為 Imagick
$im->readimageblob($image);

// 建立最大 200x82 的縮圖
$im->thumbnailImage(200,82,true);

// 新增一個細微的邊框
$color=new ImagickPixel();
$color->setColor("rgb(220,220,220)");
$im->borderImage($color,1,1);

// 輸出圖像
$output = $im->getimageblob();
$outputtype = $im->getFormat();

header("Content-type: $outputtype");
echo $output;
Eero Niemi (eero at eero dot info)
16 年前
若要載入解析度比圖片預設值更高的圖片(通常是向量圖,例如 PDF),您必須在讀取檔案之前設定解析度,如下所示:

<?php
$im
= new Imagick();
$im->setResolution( 300, 300 );
$im->readImage( "test.pdf" );
?>
carlosvanhalen7 at gmail dot com
11 年前
這裡有一個方便的函式,可以找到特定像素的第一次出現。您可以設定要尋找的顏色的容差,如果需要完全匹配,則將其設定為 0。

<?php

function findPixel($img, $r, $g, $b, $tolerance=5)
{
$original_ = new Imagick($img);
$height = 0;
$width = 0;
list(
$width, $height) = getimagesize($img);
$matrix_org = array();
$matrix_mrk = array();

for(
$x = 0 ; $x < $width ; $x++){
$matrix_org[$x] = array();
$matrix_mrk[$x] = array();
}

for(
$x = 0 ; $x < $width ; $x++ )
{
for(
$y = 0 ; $y < $height ; $y++ ){
$matrix_org[$x][$y] = $original_->getImagePixelColor($x, $y)->getColorAsString();
$colors = preg_replace('/[^-,0-9+$]/', '', $matrix_org[$x][$y]);
$colors = explode(',', $colors);
$r_org = $colors[0];
$g_org = $colors[1];
$b_org = $colors[2];

if( (
$r <= ($r_org+$tolerance) && $r >= ($r_org - $tolerance) )
&& (
$g <= ($g_org+$tolerance) && $g >= ($g_org - $tolerance) )
&& (
$b <= ($b_org+$tolerance) && $b >= ($b_org - $tolerance) ) )
{
return array(
$x, $y );
}
}
}

return
false;
}

?>
To Top