Manipulating Base64 images with PHP

Asked

Viewed 5,684 times

2

I have some Base64 encoded images in PHP. I need to resize these images by setting a maximum size for them before storing in the database.

How can I do that?

1 answer

5


Have to use base64_decode, if you go from the bank take the variable and do this:

$imageBin = base64_decode($row['imagem']); //Exemplo de variavel

Here an example with file:

<?php
//Caminho da imagem em base64
$path = '/home/amanda/imagem.jpg.bin';

/*Nota: em windows algo como c:/Users/amanda/imagem.jpg.bin*/

//Decodifica base64
$imageBin = base64_decode(file_get_contents($path));

//Grava o arquivo decodificado em um temporário
$tmpfname = tempnam(sys_get_temp_dir(), 'base64_decode_');

$handle = fopen($tmpfname, 'w');
fwrite($handle, $imageBin);
fclose($handle);

//Limpa a variavel
$imageBin = null;

$img = null;

switch (variable) {
    case 'image/png':
        $img = imagecreatefrompng($tmpfname);
    break;
    case 'image/jpeg':
        $img = imagecreatefromjpeg($tmpfname);
    break;
    case 'image/gif':
        $img = imagecreatefromgif($tmpfname);
    break;
}

if (!$img) {
    //Resimensiona a imagem
    $originalWidth  = imageSX($img);
    $originalHeight = imageSY($img);

    if($originalWidth > $originalHeight)
    {
        $widthRatio = $newWidth;
        $heightRatio = $originalHeight*($newHeight / $originalWidth);
    }

    if($originalWidth < $originalHeight)
    {
        $widthRatio = $originalWidth*($newWidth / $originalHeight);
        $heightRatio = $newHeight;
    }

    if($originalWidth == $originalHeight)
    {
        $widthRatio = $newWidth;
        $heightRatio = $newHeight;
    }

    $resizedImg = imagecreatetruecolor($widthRatio, $heightRatio);

    imagecopyresampled($resizedImg, $img, 0, 0, 0, 0, $widthRatio, $heightRatio, $originalWidth, $originalHeight);

    switch (variable) {
        case 'image/png':
            imagepng($resizedImg, $tmpfname);
        break;
        case 'image/jpeg':
            imagejpeg($resizedImg, $tmpfname);
        break;
        case 'image/gif':
            imagegif($resizedImg, $tmpfname);
        break;
    }

    $img = $resizedImg = null;

    //Codifica o arquivo manipulado pra base64
    $imageBase64 = base64_encode(file_get_contents($tmpfname));

    //Sobreescreve a imagem em base64 original original
    file_put_contents($path, $imageBase64);

    $imageBase64 = null;
}

unlink($tmpfname); //Deleta o temporário

Store images or not in the database

I don’t recommend doing this for 4 factors:

  1. It can be costly to the bank and the server, read about it at: It is wrong to write byte of images in the database?

  2. You will have to decode each image that will be displayed (although it is possible to use data URI scheme), which consumes a lot of the server

  3. Even if you use data URI scheme, yet the page will get a lot of content and will take a long time to load, read more about it in: A Base64 image loads faster than a url?

  4. Images cannot be cached and cached is a very good thing to load pages visited faster, see a good way to use caching for images and other static files (chance use apache): It is possible to use if-modified-Since with "304 not modified" without PHP

  • 1

    +1 And most importantly: if you write to the database, do not use Base64 :) Base64 in principle is format for data transmission, not for storage.

  • @Bacco I agree, but I think she did use it because of data uri scheme

  • There you usually use Base64 when playing on the screen, and not in DB (unless it is something very minimal, like kids icons etc). In the case of a file, it also does not make much sense, I see no reason. It is only occupying space (and processing, in this specific case) for no reason.

  • @Bacco exactly, probably it came to this use to try to get around another problem, ie a problem to try to solve another :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.