【问题标题】:Display a newly created thumbnail without saving first?显示新创建的缩略图而不先保存?
【发布时间】:2012-06-16 12:03:41
【问题描述】:

我正在测试一个缩略图图像创建器脚本,该脚本创建一个可以在网络浏览器中显示而无需先保存的缩略图。

目前我必须使用以下代码来显示图像:

include('image.inc.php');

    header('Content-Type: image/png');
    create_thumbnail('me.jpg', false, 200, 200);

但是如何在标准网页上显示动态生成的缩略图图像以及旁边的 html 标记?

create_thumbnail 函数:

function create_thumbnail($path, $save, $width, $height){

    // Get the width[0] and height[1] of image in an array
    $info = getimagesize($path);

    // Create a new array that just contains the width and height
    $size = array($info[0], $info[1]);

    // Check what file type the image is
    if($info['mime'] == 'image/png'){

        $src = imagecreatefrompng($path);

    }else if($info['mime'] == 'image/jpeg'){

        $src = imagecreatefromjpeg($path);

    }else if($info['mime'] == 'image/gif'){

        $src = imagecreatefromgif($path);

    }else{
        // If it isn't a good file type don't do anything
        return false;
    }

    // Create a thumbnail with the passed dimensions
    $thumb = imagecreatetruecolor($width, $height);


    $src_aspect = $size[0] / $size[1];

    $thumb_aspect = $width / $height;

    if($src_aspect < $thumb_aspect){

        // Image is tall
        $scale = $width / $size[0];
        $new_size = array($width, $width / $src_aspect);
        $src_pos = array(0, ($size[1] * $scale - $height) / $scale / 2);
    }else if($src_aspect > $thumb_aspect){

        // Image is wide
        $scale = $height / $size[1];
        $new_size = array($height * $src_aspect, $height);
        $src_pos = array(($size[0] * $scale - $width) / $scale /2, 0);

    }else{

        // Image is square
        $new_size = array($width, $height);
        $src_pos = array(0, 0);
    }

    // Stop the new dimensions being less than 1 (this stops it breaking the code). Takes which ever value is higher.
    $new_size[0] = max($new_size[0], 1);
    $new_size[1] = max($new_size[1], 1);


    // Copy the image into the new thumbnail 
    // Newly created thumbnail, image copying from, starting x-coord of thumbnail, starting y-coord of thumbnail(0,0 will fill up entire thumbnail), x-coord of original image, y-coord of original image,  width of new thumbnail, height of new thumbnail, width of original image, height of original image
    imagecopyresampled($thumb, $src, 0, 0, $src_pos[0], $src_pos[1], $new_size[0], $new_size[1], $size[0], $size[1]);

    if($save === false){
        return imagepng($thumb);
    }else{

    // Create the png image and save it to passed location
    return imagepng($thumb, $save);
    }

【问题讨论】:

    标签: php image-processing http-headers thumbnails


    【解决方案1】:

    如果我正确理解您的问题,您只需插入一个img 标签并将其src 属性设置为您的php 脚本。

    类似:

    <img src="thumbnailgenerator.php?image_path=me.jpg">
    

    【讨论】:

    • 如果我想在页面上显示多个缩略图,在这种情况下如何将变量传递给缩略图生成器脚本?
    • @crm 我不知道你现在是怎么做的,但一个简单的解决方案是使用查询字符串:thumbnailgenerator.php?image_id=5 并在你的 php 脚本中获取变量:$_GET['image_id']
    • 感谢您的替代建议,但我正在尝试使其与当前脚本一起使用。
    • @crm 只需将id 更改为您现在使用的path
    • 所以我可以像这样传递变量:&lt;img src="thumbnailgenerator.php?image_path=me.jpg&amp;save=0&amp;twidth=200&amp;theight=200"&gt; 然后使用 $_GET[]?
    【解决方案2】:

    将其转换为base64(使用base64_encode),然后显示:

    <img src="data:image/jpg;base64,iVBORw0KGgoAAAANS==" />
    

    【讨论】:

    • 我尝试了这个:$image = create_thumbnail('me.jpg', false, 200, 200); echo "&lt;img src=\"data: image/png;base64,".$image."\ /&gt;";,但没有成功。有什么建议吗?
    • @crm imagepng 返回一个布尔值并(可选...)输出原始图像流。如果要捕获该流,则必须使用输出缓冲:php.net/manual/en/function.ob-get-contents.php
    • @crm 您没有将其转换为 base64。
    • 这个答案对我最初的问题是正确的,尽管我喜欢在 url 中传递变量并使用 $_GET 的建议,谢谢。
    猜你喜欢
    • 1970-01-01
    • 2019-11-27
    • 1970-01-01
    • 2019-07-11
    • 1970-01-01
    • 2011-05-07
    • 2012-03-09
    • 1970-01-01
    • 2011-01-18
    相关资源
    最近更新 更多