简体   繁体   English

使用php动态调整图像大小

[英]resize image on the fly using php

I'v searched all day for a function to resize my pictures on the server on the fly, without saving them.The code works, it shows the full size images, now I want to make them smaller. 我整天都在寻找一个无需更改即可即时在服务器上调整图片大小的功能。代码可以正常工作,显示完整尺寸的图片,现在我想缩小图片。 This is the function: 这是功能:

function resize_image($file, $width, $height) {
    if (file_exists($file)){
        $image = imagecreatefromjpeg($file);
        list($orig_width, $orig_height) = getimagesize($file);
        $ratio = $orig_width / $orig_height;
        if ($ratio < 1) {
            $width = $height * $ratio;
        } else {
            $height = $width / $ratio;
        }
        $new_image = imagecreatetruecolor($width, $height);
        imagecopyresized($new_image, $image,
        0, 0, 0, 0,
        $width, $height,
        $orig_width, $orig_height);
        // header('Content-type: image/jpeg');
        imagejpeg($new_image, NULL, 80);
    }
}

From what I searched today I need to put the header (Content-type: image/jpeg') for the browser to recognize the output as an image but if I do that it stops the script. 从我今天进行的搜索中,我需要为浏览器添加标题(Content-type: image/jpeg') ,以将输出识别为图像,但是如果这样做,它将停止脚本。

This is the page using it: 这是使用它的页面:

<? include('resize.php');
 $chapter='test';
 $query=$db->prepare("SELECT * FROM `test_db` WHERE `test_db`.`Chapter` LIKE :? ORDER BY `id` ASC LIMIT 0, 6");
    $query->bindValue(1, $chapter, PDO::PARAM_STR);
    $query->execute();
    $rows= $query->fetchAll(PDO::FETCH_ASSOC);  
foreach ($rows as $row){       
    echo "<li><h2>".$row['Name']."</h2></li>" ;
    resize_image("_images/".$row['img_number'].".jpg", 300, 190);
  };  
?>

You cannot output html and images to the browser in the same script. 您不能使用同一脚本将html和图像输出到浏览器。

You should either: 您应该:

  • use a separate script to output the image and call that from the html like (simple example): 使用单独的脚本输出图像,然后从html调用图像(简单示例):

    <img src="your_script.php?id=XX&size_x=XX&size_y=XX">

  • save the images to a file and link to that file from your html. 将图像保存到文件,然后从html链接到该文件。

You could also encode the images as base64 strings and use that in your image tags but that would lead to a very large html file unless you are talking about simple buttons. 您还可以将图像编码为base64字符串,并在图像标签中使用它,但这将导致非常大的html文件,除非您在谈论简单的按钮。

Don't put your closing tag in PHP. 不要将结束标记放在PHP中。 It will output whitespace after the closing tag which will alter the result. 它将在结束标记之后输出空格,这将改变结果。

Since when are headers stopping the script? 从什么时候起头文件停止脚本?

And yes, the reason why resized files are saved, is (1) you'll probably need them again, (2) the content type makes them an image in stead of text. 是的,保存调整大小的文件的原因是:(1)您可能会再次需要它们;(2)内容类型使它们成为图像而不是文本。 If you want to resize those 'inline' you'll need two content types, and I guess that won't work that well... 如果您想调整那些“内联”的大小,则需要两种内容类型,我想那将不能很好地工作...

What you could do, is resize and save the file, serve it and delete it. 您可以做的是调整大小并保存文件,提供文件并删除它。 Like a temporary image file. 就像一个临时图像文件。 A more direct way doesn't exist, according to me. 据我说,没有更直接的方法。 But if I'm wrong, please point that out in the comments for me. 但是,如果我错了,请在我的评论中指出。 I like to learn new stuff ;) 我喜欢学习新东西;)

Edit: okay, EXCEPT when the script only handles the image resizing. 编辑:好的,当脚本仅处理图像调整大小时除外。 Just thinking about that one right now. 现在就想一想。 Sorry :) 对不起:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM