1

我想在显示时间的同时显示图像的缩略图,但不想保存图像。

我尝试了一些notorious ;)脚本,但它不起作用:(。请看一下,让我知道你有什么想法

<?php
    function print_thumb($src, $desired_width = 100){
        /* read the source image */
        $source_image = imagecreatefromjpeg($src);
        $width = imagesx($source_image);
        $height = imagesy($source_image);

        /* find the "desired height" of this thumbnail, relative to the desired width  */
        $desired_height = floor($height * ($desired_width / $width));

        /* create a new, "virtual" image */
        $virtual_image = imagecreatetruecolor($desired_width, $desired_height);

        /* copy source image at a resized size */
        imagecopyresampled($virtual_image, $source_image, 0, 0, 0, 0, $desired_width, $desired_height, $width, $height);


        // Set the content type header - in this case image/jpeg
        header('Content-Type: image/jpeg');

        // Output the image
        imagejpeg($virtual_image);

    }
?>

<img src="<?php print_thumb("s1.jpg"); ?>" />

我将此文件保存为thumbs.php(单个文件),同时通过 localhost 访问它,它显示如下

<img src="http://localhost/test/thumbs.php">

如果我将两者都写在单独的文件中,它工作正常。

喜欢file.html_

<img src="thumb.php?img=s1.jpg" />

拇指.php

<?php
    function print_thumb($src, $desired_width = 100){
        /* read the source image */
        $source_image = imagecreatefromjpeg($src);
        $width = imagesx($source_image);
        $height = imagesy($source_image);

        /* find the "desired height" of this thumbnail, relative to the desired width  */
        $desired_height = floor($height * ($desired_width / $width));

        /* create a new, "virtual" image */
        $virtual_image = imagecreatetruecolor($desired_width, $desired_height);

        /* copy source image at a resized size */
        imagecopyresampled($virtual_image, $source_image, 0, 0, 0, 0, $desired_width, $desired_height, $width, $height);


        // Set the content type header - in this case image/jpeg
        header('Content-Type: image/jpeg');

        // Output the image
        imagejpeg($virtual_image);

    }

    print_thumb($_REQUEST['img']);
?>
4

4 回答 4

1

您需要 2 个单独的脚本。

1 输出图像image/jpg,1 输出 HTML。您似乎正在尝试将图像直接渲染到src属性。

HTML页面:

<html>
    <body>
        <img src="http://localhost/test/thumbs.php?img=s1.jpg">
    </body>
</html>

PHP页面:

<?php
    function print_thumb($src, $desired_width = 100){
        // your function as is
    }

    // you should probably sanitize this input
    print_thumb($_REQUEST['img']);
?>
于 2013-01-04T10:53:49.370 回答
0

尝试使用“TimThumb”(点击这里),它确实让这样的事情变得非常容易。

于 2013-01-04T10:49:54.540 回答
0

如果<img src="<?php print_thumb("s1.jpg"); ?>" />是您显示的代码中 thumbs.php 的一部分,那么您必须将其删除。我还建议?>从 thumbs.php 中删除

于 2013-01-04T10:51:32.947 回答
0

在'thumb.php'里面

<?php

  function print_thumb($src, $desired_width = 100){

    if( !file_exists($src) )
      return false;

    /* read the source image */
    $source_image = imagecreatefromjpeg($src);
    $width = imagesx($source_image);
    $height = imagesy($source_image);

    /* find the "desired height" of this thumbnail, relative to the desired width  */
    $desired_height = floor($height * ($desired_width / $width));

    /* create a new, "virtual" image */
    $virtual_image = imagecreatetruecolor($desired_width, $desired_height);

    /* copy source image at a resized size */
    imagecopyresampled($virtual_image, $source_image, 0, 0, 0, 0, $desired_width, $desired_height, $width, $height);


    // Set the content type header - in this case image/jpeg
    header('Content-Type: image/jpeg');

    // Output the image
    imagejpeg($virtual_image);

    return true;

  }

  if( isset( $_GET['file'] ) && !empty( $_GET['file'] ) ){

    if( !print_thumb( $_GET['file'] ) ){

      echo 'Failed to create thumb for "'.$_GET['file'].'"';

    }else{

      // The Thumb would have been returned

    }

  }else{

    echo 'No File specified';

  }
?>

在显示缩略图的任何页面内

<img src="thumb.php?file=s1.jpg" />

您现有代码的问题是您将错误的代码放在错误的文件中。thumb.php文件什么都不做,只是查看它正在发送的参数(它打算变成缩略图的文件)并返回图像。

因此,将<img....标记放在该文件的末尾是您的问题所在。

相反,您必须查看您试图使图像出现的位置。您必须将该图像文件名传递到标记中,以便thumbs.php文件知道您希望它缩略图并返回什么。

正如另一位受访者指出的那样,有一些库可以为您做这件事,比自己编写要容易得多。

于 2013-01-04T11:09:19.603 回答