resizing image function implementation inpage

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-13 08:11:30

问题


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)

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.


回答1:


You can use a data-url for that:

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

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

echo base64_encode(...);


来源:https://stackoverflow.com/questions/44988132/resizing-image-function-implementation-inpage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!