I have three images, with the aim of making the first image a base while the two other images are placed on it. With the second image placed at the left corner (almost to the top) while the third image is placed nearly to the bottom of the right corner with a text inscription under it.
It is an API based application, I am writing to JSON (http://127.0.0.1:8000/api/upload)
More also I have tried to use intervention image but I encountered a serious problem with the error as regards array expected instead of string and more also insert() is complaining of the column not found. That is why was I used raw PHP.
The First Image should cover the black background
The second image should be placed to the right (nearly top) corner
The Third Image is expected to be placed at the left bottom corner while the text will be wrapped under it.
my controller image is rendering the image scattered with the text above the image instead of under.
public function upload(Request $request)
{
$x=$y=600;
header('Content-Type: image/png');
// $targetFolder = '/app/uploads/images/';
// $targetPath = $_SERVER['DOCUMENT_ROOT'] . $targetFolder;
$validation = $request->validate([
'title' => 'string',
'image' => 'required|file|image|mimes:jpeg,png,gif,webp|max:2048'
]);
$file = $validation['image']; // get the validated file
$extension = $file->getClientOriginalExtension();
$filename = 'mm-image-' . time() . '.' . $extension;
$path = $file->storeAs('/uploads/images', $filename);
$image = storage_path('app/uploads/images/mm-image-1552822080.png');
$c = storage_path('app/uploads/images/mm-image-1552936505.png');
$im3 = file_get_contents($request->image);
$outputImage = imagecreatetruecolor(600, 600);
// set background to white
$white = imagecolorallocate($outputImage, 255, 255, 255);
imagefill($outputImage, 0, 0, $white);
$first = imagecreatefrompng($image);
$second = imagecreatefrompng($c);
$third = imagecreatefromstring($im3);
//imagecopyresized ( resource $dst_image , resource $src_image , int $dst_x , int $dst_y , int $src_x , int $src_y , int $dst_w , int $dst_h , int $src_w , int $src_h )
imagecopyresized($outputImage,$first,0,0,0,0, $x, $y,$x,$y);
imagecopyresized($outputImage,$second,0,0,0,0, $x, $y,$x,$y);
imagecopyresized($outputImage,$third,300,300,0,0, 100, 100, $x, $y);
// Add the text
//imagettftext ( resource $image , float $size , float $angle , int $x , int $y , int $color , string $fontfile , string $text )
//$white = imagecolorallocate($im, 255, 255, 255);
$text = 'School Name Here';
$font = storage_path('app/public/tahoma.ttf');
imagettftext($outputImage, 16, 0, 300, 300, $white, $font, $text);
$filename =storage_path('app/uploads/images/'.round(microtime(true)).'.png');
imagepng($outputImage, $filename);
imagedestroy($outputImage);
}
答案 0 :(得分:1)
正如Gaurav Gupta在评论中所说,我建议您使用Intervention Image PHP软件包,该软件包将所有GD方法封装为易于使用的东西(您也可以使用Imagick)。
您可以执行以下操作:
public function upload(Request $request)
{
$x=$y=600;
$imageMerge = new ImageMerge();
header('Content-Type: image/png');
$validation = $request->validate([
'title' => 'string',
'image' => 'required|file|image|mimes:jpeg,png,gif,webp|max:2048'
]);
$file = $request->file('image')->getClientOriginalName();
$extension = $request->file('image')->getClientOriginalExtension();
$hash = Str::random(10);
$filename = 'mm-image-' . $hash . '.' . $extension;
$path = $file->storeAs('/uploads/images', $filename, ['disk' => 'public']);
$image = storage_path('app/uploads/images/mm-image-1552822080.png');
$c = storage_path('app/uploads/images/mm-image-1552936505.png');
$im3 = file_get_contents($request->title);
$logo = Image::make($file);
$background = Image::make($image);
$person = Image::make($c);
// Resize the picture to insert to the good size
$logo->resize(200, 320);
$person->resize(300, 200);
// Insert those pictures in the background to a specific position with some padding
$background->insert($person, 'left', 30, 0);
$background->insert($logo, 'right', 30, 0);
// Draw white filled rectangle for the text
$background->rectangle(0, 0, 0, 0, function ($draw) {
$draw->background('#FFFFFF');
});
// use callback to define details
$background->text($im3, 0, 0, function($font) {
$font->file(storage_path('app/public/tahoma.ttf'));
$font->size(24);
$font->color('#000000');
$font->align('center');
});
// Resize image to specific output size
$background->resize($x, $y);
$exportName = 'file-'.$hash.'.'.$extension;
Storage::disk('public')->put('/uploads/final/'.$exportName, $background);
$imageMerge->image = $exportName;
$imageMerge->save();
}
这不是完成的脚本。您仍然需要计算元素的所有确切位置并验证所有存储路径,以确保正确导入每个文件,但最终Intervention Image确实非常易于使用。
以下是我们在文档中使用的方法,以便您可以根据需要进行调整: