2013-02-25 72 views
0

我遇到以下問題。我做了一個thumbnailer的GD的,當我在Chrome中運行它,這是它做什麼:imagecopyresampled調整大小在鉻作物在ie/ff

這正是我期望它做的事(調整其大小)

screenshot taken in chrome

可悲的是,這是什麼Firefox和IE做到:(裁剪)

image taken in ff

我有以下代碼照顧我調整大小:

// this image is created by another php file when text is filled in 
$file = "hidden.png"; 
$size = GetImageSize($file); 
if($size !== false){ 
$w = $size[0]; 
$h = $size[1]; 
//set new size 
$nw = $_GET['width']; 
$nh = ($nw*$h)/$w; 
} 
else{ 
//set new size 
$nw = 400; 
$nh = 200; 
} 
//draw the image 
$src_img = imagecreatefrompng($file); 
$dst_img = imagecreatetruecolor($nw,$nh); 
imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $nw, $nh, $w, $h); 
//resizing the image 
imagepng($dst_img); 
imagedestroy($src_img); 
imagedestroy($dst_img); 

我搜索了堆棧和谷歌上的abit,唯一能找到的是使用css的解決方案,因爲我的圖像不是以這種方式構建的,所以我不需要它。

我需要做什麼代碼明智(而不是css相關),讓它在所有瀏覽器中正常工作?

,如果需要,我可以發佈更多的代碼

+0

這不太可能是瀏覽器造成的,因爲調整大小在服務器端運行。你做了一些調試,什麼值傳遞給腳本? (「寬度」等) – 2013-02-25 09:35:47

+0

+1,用於記錄良好且寫得很好的問題。好樣的! – L0j1k 2013-02-25 09:35:51

+0

@Pekka웃這裏傳遞給腳本的寬度值是300 – Robin 2013-02-25 09:37:33

回答

0

我用我的媒體鏈接現有的腳本合併上述腳本來生成圖像解決的問題。

/* Get image info */ 
if(!isset($_POST['width']) && !isset($_POST['height'])) 
{ 
$width = '200'; 
$height = '100'; 
} 
else 
{ 
$width = $_POST['width'] ; 
$height = $_POST['height']; 
} 
$Image = imagecreatetruecolor($width,$height) ; 
$sx = imagesx($Image) ; 
$sy = imagesy($Image) ; 
/*Check if RGB values have been set*/ 
if(!isset($_POST['r'])) {  $R = 255; } else {  $R = $_POST['r']; } 
if(!isset($_POST['g'])) {  $G = 255; } else {  $G = $_POST['g']; } 
if(!isset($_POST['b'])) {  $B = 255; } else {  $B = str_replace(")" , "" , $_POST['b']); } 

/*Check if the text value is set */ 
if(!isset($_POST['text'])) {$Text = "Uw tekst hier";} 
else if($_POST['text'] == '') {$Text = "Uw tekst hier";} 
else {$Text = $_POST['text'];} 

/*Check if the font is set */ 
if(!isset($_POST['font'])) {$Font="./Fonts/arial.ttf" ;} 
else{$Font= "./fonts/".$_POST['font'].".ttf";} 

$FontColor = ImageColorAllocate ($Image,$R,$G,$B) ; //TextColor 
$FontShadow = ImageColorAllocate ($Image,0,0,0) ; //BackGroundColor 
$Rotation = 0 ; 
/* Iterate to get the size up */ 
$FontSize=1 ; 
do 
    { 
    $FontSize *= 1.1 ; 
    $Box = @ImageTTFBBox($FontSize,0,$Font,$Text); 
    $TextWidth = abs($Box[2] - $Box[6]) ; 
    $TextHeight = abs($Box[3] - $Box[7]) ; 
    } 
while ($TextWidth < $sx*0.94) ; 
/* Awkward maths to get the origin of the text in the right place */ 
$x = $sx/2 - cos(deg2rad($Rotation))*$TextWidth/2; 
$y = $sy/2 + sin(deg2rad($Rotation))*$TextWidth/2 + cos(deg2rad($Rotation))*$TextHeight/2 ; 


imagefilledrectangle($Image, 0, 0, $sx , $sy , $FontShadow); 
ImageTTFText ($Image,$FontSize,$Rotation,-$Box[6] ,-$Box[7],$FontColor,$Font,$Text); 

if(isset($_POST['resize']) && $_POST['resize'] == 1) 
{ 
    $nw = 400; 
    $nh = 200;   

    $src_img = $Image; 
    $dst_img = imagecreatetruecolor($nw,$nh); 
    imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $nw, $nh, $sx, $sy);//resizing the image 
    imagepng($dst_img, "hidden.png"); 
    imagedestroy($src_img); 
    imagedestroy($dst_img); 
} 
else 
{ 
    //header('Content-type: image/png'); 
    Imagepng($Image, "hidden.png") ; 
} 
0

它絕對無關,與瀏覽器,因爲那是沒有辦法的辦法PHP的作品。 首先將php代碼解析到服務器上,然後將結果傳遞給客戶端(瀏覽器)。

所以我認爲你應該首先檢查一下圖像是否正常工作。意味着它每次產生相同的圖像。那麼你應該檢查這個部分:

if($size !== false){ 
    $w = $size[0]; 
    $h = $size[1]; 
    $nw = $_GET['width']; // <---- For Debugging set here a static number 
    $nh = ($nw*$h)/$w; 
} 

嘗試設置調試部分,每個變量可能會改變每個請求到一個固定值。

我希望我能幫助你

+0

我已經設置所有的本地變量誰可以改變與程序固定設置,但它並沒有修復任何傷心 – Robin 2013-02-25 13:43:35