私の問題は、現在のキャンバスをデータベースに JSON オブジェクトとしてアップロードするだけでなく、キャンバスからサムネイルを保存することです。一種のレイアウト ギャラリー用です。
特定のサイズへのスケールを除いて、ほぼすべてが機能しています。PHP 関数Scale Image with PHPにこのチュートリアルを使用しましたfunction scaleImg();
だからここにアップロードスクリプトからの私のコードがあります:
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)
...
これは、ファイルシステムに画像を保存できるvar lt_canvas_thumb = canvas.toDataURL("image/png");
JavaScript関数から来て
いますが、元のキャンバスのサイズ(370x522)でのみ保存できます。
スケーリング用の PHP スクリプトは次のとおりです。
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;
}
}
次のエラー メッセージが表示されます。
データ: ../flyer/templateDATA/template_1_thumbs/tmp.png
警告: imagepng() は、パラメーター 1 がリソースであると想定し、57行目の/Applications/XAMPP/xamppfiles/htdocs/flyer/helper_scaleImg.phpで指定された null
ファイルがディレクトリに正しく保存されているため、なぜ「null」なのかわかりません。
私のコードの小さな間違いではないことを願っています。誰かが私を助けてくれることを願っています。ありがとう!あいさつマックス