2012-08-12 70 views
0

因此,我正在嘗試編寫一個腳本來裁剪用戶圖像。我會發送一些信息(寬度,高度,對齊屬性和圖像url)到腳本,它應該返回裁剪圖像。然而,它不工作...只是一個「圖像未找到」符號:/這是我的劇本,有什麼想法?在php中修剪圖像

<?php 
session_start(); 
header('Content-type: image/jpeg'); 
$w=$_GET['w']; 
$h=$_GET['h']; 
$x=$_GET['x']; 
$y=$_GET['y']; 
$filename="http://www.domain.com/".$_GET['src']; 
$file_ext = substr($filename, strrpos($filename, ".") + 1); 
$ext=''; 

if($file_ext=='jpg') 
{ 
    $image = imagecreatefromjpeg($filename); 
} 
else if ($file_ext=='gif') 
{ 
    $image = imagecreatefromgif($filename); 
} 
else if ($file_ext=='png') 
{ 
    $image = imagecreatefrompng($filename); 
} 

$crop = imagecreatetruecolor($w,$h); 
imagecopy($crop, $image, 0, 0, $x, $y, $w, $h); 
imagejpeg($crop); 
?> 

編輯:看起來這是錯誤:致命錯誤:調用到第24行,domainpath/crop.php未定義功能imagecreatetruecolor()有什麼我需要做的加載這個功能呢?

+1

在[Fiddler](http://www.fiddler2.com/fiddler2/)中觀看請求/響應,響應中有空白或錯誤​​消息你看不到。 – DCoder 2012-08-12 05:49:28

+1

刪除「Content-Type」標題並用瀏覽器點擊該URL以查看錯誤是什麼。 (確保錯誤報告已打開。) – Brad 2012-08-12 05:49:29

+0

您是否使用imagick? – 2012-08-12 05:51:13

回答

1

使用ImageMagick,試試這個;

<?php 
function resize_image($file, $w, $h, $crop=FALSE) { 
    $img = new Imagick($file); 
    if ($crop) { 
     $img->cropThumbnailImage($w, $h); 
    } else { 
     $img->thumbnailImage($w, $h, TRUE); 
    } 

    return $img; 
} 
resize_image(‘/path/to/some/image.jpg’, 150, 150); 
2

根據你對錯誤評論,imagecreatetruecolor()確實是一個功能,但前提是你必須加載GD庫。

確保您的PHP安裝可以訪問GD的兼容版本,並且如果您剛剛添加了它,請不要忘記重新啓動Web服務器。