使用PHP创建图像的虚拟缩略图

时间:2013-01-04 10:47:30

标签: php

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

我已经尝试了一些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" />

thumb.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 个答案:

答案 0 :(得分:1)

您需要2个单独的脚本。

1输出图像image/jpg,输出一个输出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']);
?>

答案 1 :(得分:0)

尝试使用“TimThumb”(CLICK HERE),它确实非常容易。

答案 2 :(得分:0)

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

答案 3 :(得分: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 文件知道您希望它缩略图并返回。

正如另一位回应者指出的那样,有些图书馆会为你做这件事比你自己写的更容易。