2012-09-08 37 views
2

我的最終目標是將輸入圖像調整爲100px寬度,125px高度。一些輸入圖像是一個不同的長寬比,所以我希望他們在一個100x125的容器中,用邊緣顏色填充背景稀疏。Imagick調整大小,中心和稀疏填充問題

好了,所以這個工程的基本調整大小:

$image = new Imagick($imgFile); 
$image->resizeImage(100,0, Imagick::FILTER_LANCZOS, 1, false); 
$image->writeImage("$Dir/$game.png"); 
header("Content-type: ".$image->getImageFormat()); 
echo $image; 
$image->clear(); 
$image->destroy(); 

不過,我一直在尋找了幾個小時,我無法找到一個簡單的「這是你如何在畫布上居中圖像」位的PHP的Imagick庫。一切都是爲了實際的ImageMagick轉換應用程序,這並不是我所追求的。我已經嘗試將大小調整後的圖像合成爲具有設置寬度和高度的空白newImage,但似乎覆蓋了尺寸,而不考慮複合類型,將「重力」設置爲居中,然後100x125範圍內無效(始終坐在0,0,並試圖設定Y偏移量((125-imageheight)/ 2)導致的偏移,這是方式更比它應該是)

編輯:

$imageOutput = new Imagick(); 
    $image = new Imagick($imgFile); 
    $image->resizeImage(100,0, Imagick::FILTER_LANCZOS, 1, false); 
    $imageOutput->newImage(100, 125, new ImagickPixel('black')); 
    $imageOutput->compositeImage($image, Imagick::COMPOSITE_ADD, 0, ((125 - $image->getImageHeight()))/2); 
    $imageOutput->setImageFormat('png'); 
    $imageOutput->writeImage("$Dir/$game.png"); 
    header("Content-type: ".$imageOutput->getImageFormat()); 
    echo $imageOutput; 
    $image->clear(); 
    $image->destroy(); 

所以我讓我的中心工作,重力顯然對實際圖像沒有影響。

我完全不知道我甚至會開始嘗試重新創建一個命令行邊緣 - 在庫中使用稀疏填充PHP。

+0

你想拍攝一個圖像,比如500 * 500,並縮小到適合100 * 125的圖像?那麼,由於500 * 500不能完美縮放,只能使高度125和寬度縮放比例成正比,然後將它垂直放置在盒子內部? – jeremy

+0

我試圖在一個相同寬度的高長方形內垂直方形的中心 – fire

+0

Y =(rectangleHeight/2) - (squareHeight/2) – jeremy

回答

2

我最終使用Imagick和shell調用的組合來轉換它自己,我最終將其重寫爲使用完全的shell調用。我也改變了我的尺寸,這裏的代碼:

$imageOutput = new Imagick(); // This will hold the resized image 
    $image = new Imagick($imgFile); // Open image file 
    $image->resizeImage(120,0, Imagick::FILTER_LANCZOS, 1, false); // Resize it width-wise 
    $imageOutput->newImage(120, 150, "none"); // Make the container with transparency 
    $imageOutput->compositeImage($image, Imagick::COMPOSITE_ADD, 0, ((150 - $image->getImageHeight())/2)); // Center the resized image inside of the container 
    $imageOutput->setImageFormat('png'); // Set the format to maintain transparency 
    $imageOutput->writeImage("$Dir/$game.temp.png"); // Write it to disk 
    $image->clear(); //cleanup -v 
    $image->destroy(); 
    $imageOutput->clear(); 
    $imageOutput->destroy(); 
    //Now the real fun 
    $edge = shell_exec("convert $Dir/$game.temp.png -channel A -morphology EdgeIn Diamond $Dir/$game.temp.edge.png"); // Get the edges of the box, create an image from just that 
    $shepards = shell_exec("convert $Dir/$game.temp.edge.png txt:- | sed '1d;/0) /d; s/:.* /,/;'"); // get the pixel coordinates 
    $final = shell_exec("convert $Dir/$game.temp.edge.png -alpha off -sparse-color shepards '$shepards' png:- | convert png:- $Dir/$game.temp.png -quality 90 -composite $Dir/$game.jpg"); // Sparse fill the entire container using the edge of the other image as shepards , then composite that on top of this new image 
    unlink("$Dir/$game.temp.png"); // cleanup temp files 
    unlink("$Dir/$game.temp.edge.png"); 
    set_header_and_serve("$Dir/$game.jpg"); // serve the newly created file 
+0

然後只用一行shell: 'exec('convert -resize'。escapeshellarg($ w)。'x'。escapeshellarg($ h)。'-background escapeshellarg($ color)-gravity center -extent'。 escapeshellarg($ w)。'x'。escapeshellarg($ h)。''。escapeshellarg($ input_png_path)。''。escapeshellarg($ output_png_path));' 調整爲$ wx $ h保持比例,'extents'畫布到給定的$ w和$ h(如果需要),然後用任何背景$ color(「white」)填充其餘部分。您獲得的圖像總是與居中和比例圖像(不變形)相同的大小,休息填充$顏色。 – aesede

+0

如果沒有escapeshellargs以提高可讀性: 'exec(「convert -resize $ wx $ h -background $ color -gravity center -extent $ wx $ h $ input_path $ output_path」);' – aesede

+0

這實現了與目標不同的目標我正在拍攝 – fire