我希望在页面加载时使用PHP减少图像大小。我真的不确定如何实现这一点,我已经获得了尺寸但我如何仅使用PHP来减小这些尺寸?这是我目前的代码:
<?php
$stmt = $db->query('SELECT * FROM img_gallery ORDER BY list');
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) :
$image = $row['url'];
list($width, $height) = getimagesize($image); //grab the image dimensions here
?>
<img src="" width="<?=$width ?>px" height="<?=$height ?>px" /> //image here
那么每个尺寸减少了几百个像素?
答案 0 :(得分:0)
答案 1 :(得分:0)
您需要创建一个具有所需大小的新画布,并将重新采样的画布复制到那里:
$newcanvas = imagecreatetruecolor($width,$height);
$oldcanvas = imagecreatefromjpeg($imagePath);
imagecopyresampled($newcanvas,$oldcanvas,0,0,0,0,$width, $height,$originalwidth,$originalheight);