调整图像功能实现页面大小 [英] resizing image function implementation inpage

查看:113
本文介绍了调整图像功能实现页面大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

resize.php

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);
?>

上面是我用来调整任何图像大小的代码(例如

Above is code which i use to resize any image (e.g http://example.com/resize.php?w=480&h=320&q=50&img=http://example.com/image.jpg)

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

But i'm resizing images on just single page in my website so i wanted to implement the code inside that page as a function and put the result of that function in an <img> tag.

推荐答案

您可以为此使用数据网址:

You can use a data-url for that:

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

您的resizeImg()然后将需要回显编码如下的base64图像:

Your resizeImg() would then need to echo out the image base64 encoded like this:

echo base64_encode(...);

这篇关于调整图像功能实现页面大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆