E
E
Evgeny Konkin2015-09-28 15:23:27
PHP
Evgeny Konkin, 2015-09-28 15:23:27

PHP binary -> image?

Hi all!
I am adding/saving avatars on the site!
I crop the original image using Cropper , everything is fine, the script returns canvas, then I make an envelope in image

var $canvasImage = $(".cropper > img").cropper("getCroppedCanvas", {width: 170, height: 170});
var $img = new Image();
$img.src = $canvasImage.toDataURL("image/png");

then with ajax (on pressing the "Save" button) I pass the binary code by the html method to the handler for saving
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAKoAAACAAAAAXNSR0IArs
...
L65zz5n36pbJgMEyKpad5+99mrnHHOMb3xjzLG8f/72OLQAAAABJRU5ErkJggg==

everything is fine, BUT what I can't do is to resize and save the avatars to the folder ( domain.ru/avatars )
I tried many options, it doesn't work at all
$targetdir = "http://domain.ru/avatars/"; // указываем путь к папке сохранения
$photoname = $user["userid"] . ".jpg"; // создаем имя для будущих картинок
$photodata = $_REQUEST["photodata"]; // сохраняем значение полученного бинарного кода картинки
$originalphoto = imagecreatefromstring($photodata);
      
// берем исходное разрешение
$original_width = imagesx($originalphoto);
$original_height = imagesy($originalphoto);
            
// создаем новые пустые картинки
$new170 = imagecreatetruecolor(170, 170);
$new90 = imagecreatetruecolor(90, 90);
$new60 = imagecreatetruecolor(60, 60);
$new30 = imagecreatetruecolor(30, 30);
      
// делаем ресайз картинки
imagecopyresampled($new170, $originalphoto, 0, 0, 0, 0, 170, 170, $original_width, $original_height);
imagecopyresampled($new90, $originalphoto, 0, 0, 0, 0, 90, 90, $original_width, $original_height);
imagecopyresampled($new60, $originalphoto, 0, 0, 0, 0, 60, 60, $original_width, $original_height);
imagecopyresampled($new30, $originalphoto, 0, 0, 0, 0, 30, 30, $original_width, $original_height);
       
// сохраняем картинку с качеством - 60
ob_start();
imagejpeg($new170, $targetdir . "170-" . $photoname, 60);
imagejpeg($new90, $targetdir . "90-" . $photoname, 60);
imagejpeg($new60, $targetdir . "60-" . $photoname, 60);
imagejpeg($new30, $targetdir . "30-" . $photoname, 60);
$i = ob_get_clean(); 
      
imagedestroy($originalphoto);
imagedestroy($new170);
imagedestroy($new90);
imagedestroy($new60);
imagedestroy($new30);


Here! Tell me what's wrong with me? Thank you!

PS
$photodata = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAKoAAACAAAAAXNSR0IArs
...
L65zz5n36pbJgMEyKpad5+99mrnHHOMb3xjzLG8f/72OLQAAAABJRU5ErkJggg==";


PSS
this is before decoding
6938a6804ee14d80ad1fc245e9752b98.png
this is after base64_decode()
9b9608b29dc74275b80c44d9a43fd688.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
toxa82, 2015-09-28
@toxa82

I assume that the string data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA.......gg== for imagecreatefromstring($photodata) still needs to be decoded from base64, removing "data:image/png;base64,".

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question