我遇到了以下问题。我用gd做了一个拇指钉,当我用chrome运行它时,它就是这样做的:
这正是我所期望的(调整大小(
在chrome 中拍摄的屏幕截图
不幸的是,firefox和ie就是这么做的:(裁剪(
在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);
我在stack和谷歌上搜索了abit,唯一能找到的是使用css的解决方案,因为我的图像不是这样构建的。
我需要做哪些代码操作(与css无关(才能使其在所有浏览器中正常工作?
如果需要,我可以发布更多的代码
它绝对与浏览器无关,因为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;
}
试着为调试部分设置每个变量,每个变量可以在每次请求时更改为固定值。
我希望我能帮助你
我通过将上面的脚本与我的allready现有脚本合并来生成图像,从而解决了这个问题。
/* 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") ;
}