2011-05-24 75 views
0

我正在制作旋转横幅系统,但存在图像大小显示不一致的问题。我将图像存储在SQL表中。我知道它存储图像到SQL表中的不好的做法,但我只存储了一些文件。我需要将图像显示大小固定为150像素乘以150像素。我如何使用PHP来做到这一点?在此先感谢限制从SQL表中检索到的图像的大小

这里是我的代码至今:

<?php 
require ("connect.php"); 

$id = mysql_real_escape_string($_REQUEST['id']); 
$query = mysql_query("SELECT * FROM banner WHERE id= $id "); 
$row = mysql_fetch_array($query); 
$content = $row['image']; 
header('Content-type: image/jpg'); 
echo $content; 
?> 

<html> 
    <head> 
     <style type="text/css"> 
      a 
      { 
       text-decoration:none; 
      } 
     </style> 
    </head> 
    <body> 

    <?php 
require ("connect.php"); 

$query = mysql_query("SELECT * FROM banner"); 
$number = mysql_num_rows($query); 
$result = mysql_query("SELECT * FROM banner ORDER BY RAND() LIMIT 1 "); 

while ($row = mysql_fetch_assoc($result)) 
{ 
    echo '<a href = ' . $row[url] . '> <img src= "get.php?id= ' . $row[id] . ' "/> </a>'; 
    echo "<br />"; 
    echo '<a href = ' . $row[url] . '> ' . $row[description] . ' </a>'; 
} 

echo mysql_error(); 
?> 

    </body> 
</html> 
+0

这很简单,只需在'img'元素中添加'width'和'height'属性即可。 – 2011-05-24 08:10:57

回答

1

那么如果你有在图像控制,他们永远只需要是150像素X 150像素我建议你只需要改变大小手动将它们上传回你的数据库。

您现在必须这样做的方式意味着您每次页面加载时都会随时调整图像大小,这很糟糕。为了得到那个工作你要做的就是通的宽度和高度参数,img标签,像这样

<img src="yourimage" width="150" height="150" />

+0

一般而言,您想要添加宽度*或*高度,而不是两者。两者都会强制高宽比的突破。另外,显然我不同意你每次都要调整图像大小的感觉。这太愚蠢了。 – 2011-05-24 08:20:43

+0

@John Green - PageSpike我说他现在想做的方式很愚蠢。理想情况下,他会使用像发布或类似的脚本上传图像并调整图像大小,确保长宽比保持不变。每次他加载页面并从数据库中提取图像时,如果它不是150px x 150px,它将不得不调整大小。 – martynthewolf 2011-05-24 08:23:43

+0

这就是你缓存它的原因。我的脚本旨在实时工作。我使用一个类似的脚本作为通用的按需重新分类器。唯一的事情是你想确保你缓存图像,以便它只需要第一次完成这项工作。 – 2011-05-24 08:25:03

1

您可以使用外部库调用GD扩展您的图像。

虽然我从来没有用过它,但有一种叫做imagecreatefromstring()的东西; (我使用了这段代码的其余部分,顺便说一句....我只是从来没有存储在数据库中的图像)。

$size = 150; 
// This is a reference to your MySQL row. 
$img = imagecreatefromstring($row['image_data']); 
$width = imagesx($img); 
$height = imagesy($img); 

$aspect = $height/$width; 

// This is a simple width check limiter. There are dozens of ways to treat this, 
// so code away until you get what you want. Personally, I have a 3K line class to handle 
// all of the different ways I may want to change this scaling around. 
if ($width < $size) 
{ 
    $w = $width; 
    $h = $height; 
} 
else 
{ 
    $w = $size; 
    $h = floor($w*aspect); 
} 

$buffer = imagecreatetruecolor($w,$h); 

// There are some more advanced ways of doing this if you need an alpha channel. 
imagecopyresized($buffer,$img,0,0,0,0,$w,$h,$width,$height); 

// File is now copied and ready to go. You should save your file with a unique, but 
// reconstructable name. For instance, if your parameters to get here was 
// 'image.php?image_id=25', I'd actually md5('image.php?image_id=25') and cache it. In the 
// future, you could check your cache to see if the image was there before you even make your 
// DB call, which would speed things up greatly. 

// I show an example in the code. 

// $filename = '/var/www/public/'.md5('image.php?'.$_SERVER['query_string'].'_'.$size); 
$filename = 'test.jpg'; 
imagejpeg($buffer, $filename, 85); 

header('Content-Type: image/jpeg'); 
echo file_get_contents($filename); 

imagedestroy($buffer); 
imagedestroy($img);