PHP Merge PNG Images - php

How can i merge 2 png images, or a JPEG over a png? I have this image:
The result would have to be like this:
Is there a lightweight library that can do this, or is it possible with php functions? I just want to keep the overlay image withing the background (margin of 10px) - i like to integrate it into my api.. so i can create them on the fly - or store them for later, but being able to create/batch create them online.

Try using the Intervention Image PHP library, more specifically the insert function.
From the documentation:
Paste a given image source over the current image with an optional position and a offset coordinate. This method can be used to apply another image as watermark because the transparency values are maintained.
And an example:
// create new Intervention Image
$bg = Image::make('public/background.jpg');
// create a new Image instance for inserting
$logo = Image::make('public/logo_discovery.png');
// Insert the logo onto the background
$bg->insert($logo, 'center');

Related

How To Add An Image Above Another using PHP

I Have A Background Image Now I want PHP to Put Another Image On Top Of It (The Other Image Is Stored in A Variable)
Example variable $x
& My Background Image Is Also A Variable $back
Example ../img/back.jpg
Now i wish to Add The $x On The Left side of The Background
How May I Achieve this?
Like In This Pic There is The Green Part with a Shadow Image
How Can I replace that PART with another Picture using PHP?
(https://i.stack.imgur.com/IjK0o.jpg)
What i Have so Far
<?php
copy("https://graph.facebook.com/FACEBOOKID/picture?width=99&height=99", "picture.jpg");
$x = "picture.jpg";
copy("https://i.stack.imgur.com/IjK0o.jpg","bg.jpg");
$back = "bg.jpg";
?>
Combining images is part of image processing. You can use the gd library directly. However I recommend using an OO image processing library like intervention image, which is easier to write and understand.
// open the background image file
$img = Image::make('bg.jpg');
// Add the facebook image
$img->insert('picture.jpg');
// Save the image as a new file
$img->save('newpicture.jpg');
Read the documentation about the insert method to understand how to position the facebook image.

Resize a PHP GD-generated image in PHP and display it

I am making an avatar script from scratch and am having some problems. I got transparency working, and multi-image support for heads, bodies, shirts, etc.
Anyhow, I want to be able to generate specific sizes of the avatar within the PHP script. At this time, I have the variable $baseImage, which is an image generated using the GD script below:
$baseImage = imagecreatefrompng($startAsset);
imagealphablending($baseImage, true);
imagesavealpha($baseImage, true);
... combine all images into $base here
header("Content-type: image/png");
imagepng($baseImage);
The size of the image this generates is 350x550 (pixels) and I want to be able to get a smaller size.
I've done research but cannot find a working solution. What built-in PHP GD functions can resize this, retain transparency, and keep the great quality/colors?
There is no way to change the size of an image resource directly. Instead, you need to create a new image of the desired size and use imagecopyresampled to copy from the fullsize image to the resized one.

Convert an image from progressive to interlaced without losing quality or details?

I'm trying to retrieve large images from a directory outside of the root directory. At the moment I just use "fpassthru", but this loads the image either progressively or interlaced depending on what is was when uploaded.
How do I create a complete copy of an image but convert it to interlaced without losing any quality or detail with PHP?
If you use the GD libraries that come with PHP, you can use imageinterlace to accomplish this.
Here's from the example:
<?php
// Create an image instance
$im = imagecreatefromgif('php.gif');
// Enable interlancing
imageinterlace($im, true);
// Save the interlaced image
imagegif($im, './php_interlaced.gif');
imagedestroy($im);
?>
Alternately, you could use ImageMagick.

PHP - get blob image size - FPDF - getimagesizefromstring doesn't work

So I am using php/oracle to create a PDF via the FPDF class/plugin. I am using the Mem_Image class/script to add a blob image to my PDF, but I am having trouble determining the size of the image. I am running PHP 5.2 so getimagesizefromstring isn't working.
For the image it will have a fixed height that I am able to set in the FPDF class, but the width since it could be portrait - or - landscape image, I'll need to scale the image proportionally.
Use imagecreatefromstring and imagesx.
getimagesize() does work with stream wrappers. Unfortunately, php://memory doesn't give you a way to reference it by filename. Either create your own stream wrapper that work smarter or use the VariableStream example.

PHP: how to create an image from another PNG image

I have a small Minecraft server where people can upload their skins. Minecraft skins are small png images. Is it possible to convert this png image to another png image via PHP (e.g. GD library)?
I have made this image to help me explain what I am trying to do:
Yes, it's possible. You'd need multiple imagecopy commands to pull out sections of the skin image and paste it into the proper spots in the "output" image.
Basic order of operations would be:
$input = imagecreatefrompng('skin.png');
$output = imagecreatetruecolor(800, 600); // whatever the dimensions should be.
imagecopy($output, $input, 0,0, 10,20, 50,60);
imagecopy(...);
...
...
The first copy command is saying "take a 50x60 section of the input image, starting at coordinates 10x20, and paste it into the destination image in the top left corner".
The actual sequence/coordinates/sizes will be up to you to figure out.
If you're not doing a 1:1 copy of the image and are doing resizing, then you'll want imagecopyresampled() instead.
Here is the PHP manual for creating images from png :
http://php.net/manual/en/function.imagecreatefrompng.php
Here is a simple tutorial :
http://www.phptutorial.info/?imagecreatefrompng
You can do this with CSS
Here is a tutorial: http://www.w3schools.com/css/css_image_sprites.asp

Categories