Shuvro
Shuvro

Reputation: 1499

Failed to show the resized image in a PDF using PHP

I am trying to load an image from an external URL, then resize it and show it in a PDF. I am trying to achieve it using a single image for now, but the whole functionality will be inside a foreach loop for handling a lot of very large images.

Firstly, I resized the image, then get the contents of the image, applied base65 encoding, built up a source string from that and added the string to my img src tag. Here's my code -

    $filename = 'https://jooinn.com/images/nature-319.jpg'; // URL of the image
    $percent = 0.25; // percentage of resize

    // Content type
    header('Content-type: image/jpeg');

    // Get new dimensions
    list($width, $height) = getimagesize($filename);
    $new_width = $width * $percent;
    $new_height = $height * $percent;

    // Resample
    $image_p = imagecreatetruecolor($new_width, $new_height);
    $image = imagecreatefromjpeg($filename);
    imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

    // Output
    $imageData = base64_encode(file_get_contents($image_p));

    // Format the image SRC:  data:{mime};base64,{data};
    $src = 'data: '.mime_content_type($image_p).';base64,'.$imageData;

    // Echo out a sample image
    echo '<img src="' . $src . '">';
    imagedestroy($image_p);

I think the problem is with this line $imageData = base64_encode(file_get_contents($image_p));, I'm doing it wrong. It works nicely with URL's but how can I make it work for a resized image here? For example, the following works perfectly as long as I don't use the resized image -

    $filename = 'https://jooinn.com/images/nature-319.jpg'; // URL of the image
    // Output
    $imageData = base64_encode(file_get_contents($filename));

    // Format the image SRC:  data:{mime};base64,{data};
    $src = 'data: '.mime_content_type($filename).';base64,'.$imageData;

    // Echo out a sample image
    echo '<img src="' . $src . '">';

Upvotes: 0

Views: 53

Answers (1)

Laurent Choulette
Laurent Choulette

Reputation: 331

Indeed, as you said, the following line in the code is wrong:

$imageData = base64_encode(file_get_contents($image_p));

The $image_p variable is not a filename but a resource created by imagecreatetruecolor. You first have to convert it to a jpeg file using imagejpeg(). You can avoid saving an intermediate file before encoding to base64 using the ob_*xxx* functions

ob_start();
imagejpeg($image_p);
$imageContent = ob_get_contents();
$imageData = base64_encode($imageContent);
ob_end_clean();

You also have a problem with this line, again as $image_p is not a filename:

$src = 'data: '.mime_content_type($image_p).';base64,'.$imageData;

As you are creating a jpeg file you should just replace it with:

$src = 'data: image/jpeg;base64,'.$imageData;

For convenience here is the full working script:

$filename = 'https://jooinn.com/images/nature-319.jpg'; // URL of the image
$percent = 0.25; // percentage of resize

// Content type
header('Content-type: image/jpeg');

// Get new dimensions
list($width, $height) = getimagesize($filename);
$new_width = $width * $percent;
$new_height = $height * $percent;

// Resample
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

// Output
ob_start();
imagejpeg($image_p);
$imageContent = ob_get_contents();
$imageData = base64_encode($imageContent);
ob_end_clean();

// Format the image SRC:  data:{mime};base64,{data};
$src = 'data: image/jpeg;base64,'.$imageData;

// Echo out a sample image
echo '<img src="' . $src . '">';
imagedestroy($image_p);

Upvotes: 1

Related Questions