簡體   English   中英

PHP圖像調整大小不起作用

[英]PHP image resize isn't working

我從PHP網站找到了以下腳本,其大小僅為圖像的一半。 我使用數據庫來獲取圖像的鏈接。 其他事情正常運行,這意味着除此錯誤外,沒有任何其他錯誤。

echo "<img src='".// File and new size
$filename = '$row["image"]';
$percent = 0.5;

// Content type
header('Content-Type: image/jpeg');

// Get new sizes
list($width, $height) = getimagesize($filename);
$newwidth = $width * $percent;
$newheight = $height * $percent;

// Load
$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($filename);

// Resize
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

// Output
imagejpeg($thumb);
"'>"

錯誤:整個頁面被破壞,圖像鏈接斷開。
希望你們能幫助我!

在您刪除這些行后,它將起作用:

echo "<img src='".// File and new size

"'>"

header會為您完成此操作,並通知有jpg的到來,無需回顯圖像標簽。

另一種解決方案是刪除此行:

header('Content-Type: image/jpeg');

並創建新文件,然后將其用作圖像源:

// Output
$new_filename = 'new_image.jpg';
imagejpeg($thumb,$new_filename);//saves new image to a file, instead of outputting it to the screen
echo "<img src='$new_filename'>";

試試這個工作

<?php

$filename = '$row["image"]';
$percent = 0.5;

// Content type
header('Content-Type: image/jpeg');

// Get new sizes
list($width, $height) = getimagesize($filename);
$newwidth = $width * $percent;
 $newheight = $height * $percent;

// Load
$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($filename);

// Resize
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

// Output

echo "<img src='".imagejpeg($thumb)."'>";
?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM