简体   繁体   中英

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. 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

<?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); But I don't really understand what is this even for. Any ideas?

Your create_thumbnail function has the following return statement

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.

And then you use that returned bool value for your <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.

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">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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