使用PHP调整图像大小而不使用标题

时间:2013-12-17 02:16:39

标签: php html css image magento

我的Magento网站上有一条新闻源。我将Feed显示为带有图像的侧栏上的列表。现在,当您右键单击并打开它时,我希望源的图像为缩略图大小。现在,它是600x400,但我希望它大约是95x60。

我知道如何调整magento图像的大小,但图像来自不同的非magento网站。所以我在经过一番研究之后为此做了些什么:

foreach($channel as $item) {
    preg_match('/<*img[^>]*src *=["\']?([^"\']*)/i', $item->content, $matches);
    foreach($i = 0; $i <= count($item->category); $i++) {
        if($i < 2) {
            $file = $matches[1];
            list($width, $height) = getimagesize($file);
            $image = imagecreatefromjpeg($file);
            $new_image = imagecreatetruecolor(60,95);
            imagecopyresampled($new_image, $image, 0, 0, 0, 0, 60, 95, $width, $height);
            $image = $new_image;
            echo "<img src='data:image/jpeg;base64,".base64_encode(imagejpeg($image))."' />";
        }
    }
}

我想在调整大小后立即显示图像,因此数据:image / jpeg; base64部分,但奇怪的是这对我不起作用。为什么是这样?如何在不使用标题且不保存图像的情况下调整图像大小。我已经坚持这个问题超过一天了。请指教。

我知道我可以在html中使用with和height,但这与我想要实现的不同。我需要它永久调整大小,以便如果您尝试从列表中下载图像,它仍将是缩略图大小(95x60)而不是正常大小(600x400)。基本上(希望这很清楚),如果有人试图下载或打开图像,我需要制作缩略图95x60的实际大小。

1 个答案:

答案 0 :(得分:1)

改变这个:

$image = $new_image;
echo "<img src='data:image/jpeg;base64,".base64_encode(imagejpeg($image))."' />";

对此:

ob_start();
imagejpeg($new_image);
$image = ob_get_contents(); 
ob_end_clean();
echo "<img src='data:image/jpeg;base64,".base64_encode($image)."' />";

你应该完成:)