Level 73
So basically you are trying to watermark the image?
I would suggest using intervention for that.
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I'm trying to merge two images together but it's not working, the images dont get merged at all.
This is my full code, whats happening is:
public function getCanvas(Request $request, $id, $remake = false)
{
$reportImage = SurveyReportImage::with('surveyReportObjects.ObjectDetectionLabel.labelable')->findOrFail($id);
$filenameRectangles = 'rectangles-' . $id . '.png';
$basePath = 'img/reportImages';
$fileDirectory = storage_path('app/public/' . $basePath);
$fullFilepathRectangles = storage_path('app/public/' . $basePath) . '/' . $filenameRectangles;
$downloadPathRectangles = '/public/' . $basePath . '/' . $filenameRectangles;
if (Storage::disk('public')->exists($reportImage->path)) {
if (!Storage::disk('public')->exists($basePath . '/' . $filenameRectangles) || $remake) {
//doesn't exist or needs to be rewritten
if (Storage::disk('public')->exists($basePath . '/' . $filenameRectangles) && $remake) {
//remove existing image if being rewritten
Storage::disk('public')->delete($basePath . '/' . $filenameRectangles);
}
$reportImageFileSize = getimagesize(storage_path('app/public/') . $reportImage->path);
$image = imagecreate($reportImageFileSize[0], $reportImageFileSize[1]);
$black = imagecolorallocate($image, 0, 0, 0);
imagecolortransparent($image, $black);
//get objects for image
$objects = $reportImage->surveyReportObjects->groupBy(function ($item) {
switch ($item->ObjectDetectionLabel->labelable_type) {
case "App\\Pivots\\Tenant\\ProductFrontType":
return $item->ObjectDetectionLabel->labelable->product->subBrand->id;
case "App\\Pivots\\Tenant\\BrandFrontType":
return $item->ObjectDetectionLabel->labelable->brand->id;
case "App\\Pivots\\Tenant\\SubBrandFrontType":
return $item->ObjectDetectionLabel->labelable->subBrand->id;
}
});
$colors = $this->getColors($image, $objects->count());
$objects->each(function ($object) use ($colors, $image) {
$colorI = 0;
$object->each(function ($item) use ($colors, $image, $colorI) {
imagerectangle($image, $item->left, $item->top, $item->left + $item->width, $item->top + $item->height, $colors[$colorI]);
});
$colorI++;
});
$realImageExt = pathinfo($reportImage->path, PATHINFO_EXTENSION);
$baseLayer = null;
if ($realImageExt === 'jpg') {
$baseLayer = imagecreatefromjpeg(storage_path('app/public/' . $reportImage->path));
} else if ($realImageExt === 'png') {
$baseLayer = imagecreatefrompng(storage_path('app/public/' . $reportImage->path));
}
File::ensureDirectoryExists($fileDirectory);
imagepng($image, $fullFilepathRectangles);
$topLayer = imagecreatefrompng($fullFilepathRectangles);
//merge images together
imagealphablending($topLayer, 1);
imagealphablending($baseLayer, 1);
imagecopy(
$topLayer,
$baseLayer,
0,
0,
0,
0,
$reportImageFileSize[0],
$reportImageFileSize[1]
);
imagedestroy($image);
return Storage::download($downloadPathRectangles);
}
} else {
return response("does not exist.", 404);
}
}
Please or to participate in this conversation.