Manipulate images in base64 with PHP

2

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

How can I do this?

    
asked by anonymous 07.07.2016 / 16:35

1 answer

5

You have to use base64_decode , if it's from the bank, grab the variable and do this :

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

Here is an example with a 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 do not recommend doing this by 4 factors:

  • It can be expensive for the bank and server, read about it at: It is wrong to write byte images in the database?

  • You will have to decode each image that will be displayed (although it is possible to use data URI scheme ), which is very server consuming

  • Even if you use data URI scheme , the page will still contain a lot of content and will be slow to load, read more about it at: A base64 image loads faster than a url?

  • Images can not have cache and cache is a very good thing to load pages visited faster, see a good way to use cache for images and other static files (maybe use apache): If-modified-since with "304 not modified" without PHP

    li>
  • 07.07.2016 / 16:49