my issue is to upload the current canvas not only as a JSON Object in my Database, I also want to save a thumbnail from the canvas. It is for a kind of layout gallery.
Nearly everything is working except the scale to e specific size.
I used this Tutorial for the PHP function function scaleImg();
Scale Image with PHP
So here is my Code from the upload script:
layoutupload.php
...
//Save canvas as thumbnail
//datafields
$lt_dir = "../flyer/templateDATA/template_1_thumbs/";
$lt_canvas_thumb = $_POST['lt_canvas_thumb'];
//delete header
$lt_canvas_thumb = str_replace('data:image/png;base64,','', $lt_canvas_thumb);
$lt_canvas_thumb = str_replace(' ', '+', $lt_canvas_thumb);
//decode to an image
$img_data = base64_decode($lt_canvas_thumb);
$file = $lt_dir.'tmp.png';
//save in filesystem
$success = file_put_contents($file, $img_data);
print $success ? $file : 'Unable to save the file.';
//scale img with helper class
include "helper_scaleImg.php";
scaleImg($file,100,141,'png'); // ($img,$width,$height,$type)
...
This var lt_canvas_thumb = canvas.toDataURL("image/png");
comes from a JavaScript function
I can save the image on the filesystem but only with the original canvas dimensions (370x522).
Here is the PHP script for scaling
function scaleImg($img, $width, $height, $type){
//$type = exif_imagetype($img);
//Possible types
//IMAGETYPE_JPEG
//IMAGETYPE_PNG
if($type == 'jpg'){
$source_image = imagecreatefromjpeg($img);
// Get the size of img
$source_imagex = imagesx($source_image); //width
$source_imagey = imagesy($source_image); //height
// destination image
$dest_imagex = $width;
$dest_imagey = $height;
$dest_image = imagecreatetruecolor($dest_imagex, $dest_imagey);
//best quality (slow)
imagecopyresampled($dest_image, $source_image, 0, 0, 0, 0,
$dest_imagex, $dest_imagey, $source_imagex, $source_imagey);
return imagejpeg($dest_image,NULL,100);
}
else if($type == 'png'){
$source_image = imagecreatefrompng($img);
if(!$source_image){
// Get the size of img
$source_imagex = imagesx($source_image); //width
$source_imagey = imagesy($source_image); //height
// destination image
$dest_imagex = $width;
$dest_imagey = $height;
$dest_image = imagecreatetruecolor($dest_imagex, $dest_imagey);
//best quality (slow)
imagecopyresampled($dest_image, $source_image, 0, 0, 0, 0,
$dest_imagex, $dest_imagey, $source_imagex, $source_imagey);
/* Output an error message */
imagestring($source_image, 1, 5, 5, 'Error loading ' . $img);
}
return imagepng($dest_image,NULL,9);
//return $dest_image;
}
else
{
echo "Invalid image type!";
exit;
}
}
I'm getting the following error message:
Data: ../flyer/templateDATA/template_1_thumbs/tmp.png
Warning: imagepng() expects parameter 1 to be resource, null given in /Applications/XAMPP/xamppfiles/htdocs/flyer/helper_scaleImg.php on line 57
I don't know why it's "null", because the File is correctly saved in my directory.
I hope it issn't a small mistake in my code, and I hope someone can help me.
Thanks!
Greetings Max
`IHDRd���HI�IDATx����sGz���3����� �$DQ�ˇd[���ҮwsTj�<�!�X�a���u�v����iY� �2�C$���`�< J��*�HJd�W,�`@0����z�a�av�!��E#V{��*����BZ�Z��� ���H�N���F����㠉�s�/��(4����X���SW>�h�U��L\����lN��Oڵ�p��w��$;{�~�o���Bg���1� B��k�f8c�����.^����Wgs�V�;P4camǦM���E�~��#胈��,g�t���04U~��/�r���;8~�V.x��$IU�[3���vGǦ�# �Q+~�����ru!���lu�O_�NY������B���P,92Y��AR�r!}�SMU�ԈV|pBhV����wJm`
This looks like the base64 data. And the image is save in the directory with the "old" dimensions. – droiddude Jan 07 '13 at 23:05