调整图像功能实现页面大小

时间:2017-07-08 16:06:33

标签: php gd image-resizing

resize.php

<?php
function resizeImg($new_width, $new_height, $get_image, $quality){
    ini_set("allow_url_fopen", 1);
    list($old_width, $old_height) = getimagesize($get_image);
    $image_p = imagecreatetruecolor($new_width, $new_height);
    $image = imagecreatefromjpeg($get_image);
    imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $old_width, $old_height);
    header('Content-Type: image/jpeg');
    imagejpeg($image_p, NULL, $quality);
}
$new_width = $_GET['w'];
$new_height = $_GET['h'];
$get_image = $_GET['img'];
$get_quality = $_GET['q'];
if($get_quality == NULL){$quality = "80";}
else{$quality = $get_quality;}
resizeImg($new_width, $new_height, $get_image, $quality);
?>

以上是我用来调整任何图片大小的代码(例如http://example.com/resize.php?w=480&h=320&q=50&img=http://example.com/image.jpg

但是我在网站的单个页面上调整图像大小,所以我想将该页面中的代码实现为一个函数,并将该函数的结果放在<img>标记中。

1 个答案:

答案 0 :(得分:0)

您可以使用数据网址:

<img src="data:image/jpeg;base64,<?php echo resizeImg(...) ?>"/>

您的resizeImg()将需要回显出像这样编码的图像base64:

echo base64_encode(...);