rancor1223
rancor1223

Reputation: 356

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?

Upvotes: 0

Views: 73

Answers (3)

Nanhe Kumar
Nanhe Kumar

Reputation: 16307

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

Upvotes: 0

newman
newman

Reputation: 2719

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.

Upvotes: 0

Trogvar
Trogvar

Reputation: 856

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

Upvotes: 1

Related Questions