【问题标题】:resizing image function implementation inpage页面内调整图片功能实现
【发布时间】:2017-07-08 16:06:32
【问题描述】:

调整大小.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

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

【问题讨论】:

    标签: php gd image-resizing


    【解决方案1】:

    您可以为此使用 data-url:

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

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

    echo base64_encode(...);
    

    【讨论】:

    • 感谢您的回答,我尝试实现它但失败了,我不是这方面的专家。所以你能用勺子喂我如何准确地实现它吗
    猜你喜欢
    • 2011-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-02
    • 2021-07-21
    • 2016-06-17
    • 1970-01-01
    相关资源
    最近更新 更多