简体   繁体   English

缩略图生成不会输出任何内容

[英]Thumbnail generation won't output anything

I am trying to create simple thumbnail generation. 我正在尝试创建简单的缩略图生成。 I based it on another question here on Stack Overflow, but simplified the code for my needs. 我基于Stack Overflow的另一个问题,但是根据我的需要简化了代码。 It's supposed to take an image and shrink it based only on height. 应该拍摄图像并仅根据高度缩小它。

function create_thumbnail($original_pic, $intended_heigth){
$info = getimagesize($original_pic);
$actual_width = $info[0];
$actual_height = $info[1];

if($info['mime'] == 'image\jpeg'){
    $src = imagecreatefromjpeg($original_pic);
}else{
    return false;
}

$ratio = $intended_heigth / $actual_height;  
$newheight = $intended_heigth;
$newwidth = $actual_width * $ratio; 
$writex = 0;
$writey = 0;

$thumbnail = imagecreatetruecolor($newwidth, $newheight);
imagecopyresized($thumbnail, $src, $writex, $writey, 0, 0, $newwidth, $newheight, $actual_width, $actual_height);
return imagejpeg($thumbnail);
}

And then I am trying to echo id like this 然后我试图像这样回显id

<?php $original_pic = "images/info/7/01.jpg"; ?>
<img src="<?php create_thumbnail($original_pic, 90); ?>">

And this does nothing. 这什么也没做。 But in the original code, there was the $writex defined this way $writex = round(($mintednded_width - $newwidth) / 2); 但是在原始代码中,以这种方式定义了$ writex $ writex = round((($ mintednded_width-$ newwidth)/ 2); But I don't really understand what is this even for. 但是我什至不明白这是干什么的。 Any ideas? 有任何想法吗?

Your create_thumbnail function has the following return statement 您的create_thumbnail函数具有以下return语句

return imagejpeg($thumbnail);

If you read the documentation for imagejpeg function, you will see that it returns a bool - whether image was created successfully or not. 如果您阅读了imagejpeg函数的文档 ,则将看到它返回布尔值-图像是否成功创建。

And then you use that returned bool value for your <img> 然后将返回的bool值用于<img>

<img src="<?php create_thumbnail($original_pic, 90); ?>">

What you want to do is return the path to which the generated thumbnail was saved. 您要做的是返回生成的缩略图保存到的路径。 Read the documentation, pay attention to the second parameter of the imagejpeg function and use it to return path to saved thumbnail. 阅读文档,注意imagejpeg函数的第二个参数,并使用它返回保存的缩略图的路径。

Good luck 祝好运

You incorrect use this function. 您不正确地使用了此功能。

This function make thumbnail and return small image. 此功能制作缩略图并返回小图像。 You can store this image to file and then use link to this new image in your code. 您可以将该图像存储到文件中,然后在代码中使用指向该新图像的链接。

You should change logic of your code. 您应该更改代码的逻辑。

create image.php write bellow code
<?php
header('Content-Type: image/jpeg');

function create_thumbnail($original_pic, $intended_heigth) {
    $info = getimagesize($original_pic);
    $actual_width = $info[0];
    $actual_height = $info[1];

    if ($info['mime'] === 'image/jpeg') {
        $src = imagecreatefromjpeg($original_pic);
    } else {
        return false;
    }

    $ratio = $intended_heigth / $actual_height;
    $newheight = $intended_heigth;
    $newwidth = $actual_width * $ratio;
    $writex = 0;
    $writey = 0;

    $thumbnail = imagecreatetruecolor($newwidth, $newheight);
    imagecopyresized($thumbnail, $src, $writex, $writey, 0, 0, $newwidth, $newheight, $actual_width, $actual_height);
    return imagejpeg($thumbnail);
}
?>
<?php  $original_pic = "images/info/7/".$_GET['img']; ?>
<?php  create_thumbnail($original_pic, 90); ?>

Now you can call image with other file
<img src="image.php?img=01.jpg">

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

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