再次嘿......那些可能已经阅读过我几分钟前解决的问题>。<
动态PHP脚本运行完美,但是当我将新图像上传到我自己创建的图库时,图像会根据我的需要调整为150 x 150 ...但是,当它出现时添加新图像全是黑色...
alt text http://i48.tinypic.com/2qth175.png
您可以看到上传到该文件夹的三个黑色图像以及添加到数据库的目录。
其他(非黑色图像)已经使用image.php调整大小。
造成这种情况的原因是什么?
如果我查看源代码,代码就可以了...... PHP中的while循环生成如下输出:
<div class="view-wrap" id="photo-10">
<div class="view-icon">
<div class="img-label">
<a href="#" id="10" class="delete"><img src="img/small-delete.png" /> Delete</a>
</div>
<a href="img/events/Paintballing/24251_1395408043148_1170626626_1204038_5382765_n.jpg">
<img src="image.php?dir=img/events/Paintballing/24251_1395408043148_1170626626_1204038_5382765_n.jpg" alt="" width="110" height="110" />
</a>
</div>
</div>
一个块的示例。
如果我查看源代码(在Firefox中)并点击image.php?dir = img / events / Paintballing / 24251_1395408043148_1170626626_1204038_5382765_n.jpg exmaple,我可以看到缩略图的尺寸为150 x 150,但在布局中,它显示了一个黑色缩略图......
有谁知道为什么会这样?
编辑:
<?php
$dir = $_GET['dir'];
header('Content-type: image/jpeg');
$create = imagecreatetruecolor(150, 150);
$img = imagecreatefromjpeg($dir);
list($width, $height) = getimagesize($dir);
imagecopyresampled($create, $img, 0, 0, 0, 0, 150, 150, $width, $height);
imagejpeg($create, null, 100);
?>
这是image.php。
答案 0 :(得分:0)
感谢您更新帖子。
您是否肯定该图片是您上传的jpg / jpeg。
尝试更改为以下
<?php
$dir = $_GET['dir'];
$ext = strtoupper(pathinfo($dir, PATHINFO_EXTENSION));
switch($ext)
{
case 'jpeg':
case 'jpg':
$img = imagecreatefromjpeg($dir);
break;
case 'png':
$img = imagecreatefrompng($dir);
break;
case 'gif':
$img = imagecreatefromgif($dir);
break;
}
if(isset(img))
{
header('Content-type: image/jpeg');
$create = imagecreatetruecolor(150, 150);
list($width, $height) = getimagesize($dir);
imagecopyresampled($create, $img, 0, 0, 0, 0, 150, 150, $width, $height);
imagejpeg($create, null, 100);
}else
{
echo sprintf('Unable to process image, Unknown format %s',$ext);
}
?>
答案 1 :(得分:0)
而不是拉伸图像,为什么不添加边框?
这是函数
function resize_to_canvas($filename,$canvas_w=100,$canvas_h=225){
list($width, $height, $type) = getimagesize($filename);
$original_overcanvas_w = $width/$canvas_w;
$original_overcanvas_h = $height/$canvas_h;
$dst_w = round($width/max($original_overcanvas_w,$original_overcanvas_h),0);
$dst_h = round($height/max($original_overcanvas_w,$original_overcanvas_h),0);
$dst_image = imagecreatetruecolor($canvas_w, $canvas_h);
$background = imagecolorallocate($dst_image, 255, 255, 255);
imagefill($dst_image, 0, 0, $background);
$src_image = imagecreatefromjpeg($filename);
imagecopyresampled($dst_image, $src_image, ($canvas_w-$dst_w)/2, ($canvas_h-$dst_h)/2, 0, 0, $dst_w, $dst_h, $width, $height);
imagegif($dst_image, $filename);
imagedestroy($dst_image);}
此功能将替换原始文件,但可以轻松修改以创建新的缩略图图像。只需将文件名更改为line imagegif($ dst_image,$ filename);