Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在php中创建缩略图并显示而不保存_Php_Image_Thumbnails - Fatal编程技术网

在php中创建缩略图并显示而不保存

在php中创建缩略图并显示而不保存,php,image,thumbnails,Php,Image,Thumbnails,我有一个图像数组 $images[ 'image1.jpg', 'image2.jpg', 'image3.jpg', 'image4.jpg', 'image5.jpg', 'image6.jpg', 'image7.jpg', ..... 'image2500.jpg' ]; 这是我显示图像的循环 <?php foreach($images as $img) { ?> <img src = "path_to_my_image/<?=$img;?>"&

我有一个图像数组

$images[
'image1.jpg',
'image2.jpg',
'image3.jpg',
'image4.jpg',
'image5.jpg',
'image6.jpg',
'image7.jpg',
 .....
'image2500.jpg'
];
这是我显示图像的循环

<?php foreach($images as $img) { ?>
    <img src = "path_to_my_image/<?=$img;?>">
<?php } ?>

">
一切都很好,但在我的阵列中,有时它可以是2500-3000个图像,而且工作速度很慢

如何在循环中创建缩略图而不保存和显示? 或
如何解决此问题?

听起来您应该压缩图像! 这里是一个优化服务的免费试用版,但很多都存在,并且可以自己编写代码

这里有一个PHP包,如果需要,可以在运行时执行


如果您想使用PHP为每个图像生成缩略图而不保存缩略图,那么您可以尝试这样一个简单的脚本

<?php
    /* simplethumb.php */
    $file=!empty( $_GET['file'] ) ? $_GET['file'] : false;
    $percent=!empty( $_GET['percent'] ) ? $_GET['percent'] : 10;


    try{
        if( !$file ) throw new Exception('Source file does not exist or cannot be found');
        $source = @imagecreatefromjpeg( $file );

        if( $source && file_exists( $file ) ){
            clearstatcache();

            list( $w, $h, $t, $a ) = getimagesize( $file );
            $nw = $w * ($percent/100);
            $nh = $h * ($percent/100);

            $thumb = imagecreatetruecolor( $nw, $nh );
            @imagecopyresized( $thumb, $source, 0, 0, 0, 0, $nw, $nh, $w, $h );

            header('Content-type: image/jpeg');
            @imagejpeg( $thumb );
            @imagedestroy( $thumb );
        }
    }catch( Exception $e ){
        header('HTTP/1.1 400 Bad Request',true,400);
        exit( $e->getMessage() );
    }
?>

为了你的循环

foreach($images as $img) {
    echo "<img src='simplethumb.php?file=path_to_my_image/{$img}&percent=25' />";
}
foreach($img形式的图像){
回声“;
}

使用php生成每个图像的缩略图可能会降低整个过程的速度