Php Photos Edit - php

Hello i have a folder with a 3000 photos and i need a script that add my site logo buttom the image, i have script to edit images but i need a one that open images and make the procces,
my script
<?php
$logo = imagecreatefrompng("logo.png");
header('Content-type: image/jpg');
$image = imagecreatefromjpeg("image.jpg");
imagecopy($image, $logo, 132, 95, 0, 0, 25, 25);
imagejpeg($image);
imagedestroy($image);
?>
also if anybody have a program for windows please send to me.

I would do it like this:
function add_png_logo(){
$dir = APPPATH.'assets/img/'; # Where your PNG photos are
$dirUpload = APPPATH.'assets/uploads/'; # Where they will be saved
$baseUrl = 'http://127.0.0.1/tests/assets/uploads/'; # Base to link merged images
# Where the work begins
$iteractor = new DirectoryIterator($dir);
$ext = ['png'];
$i = 0;
# Function to save individual files
function save_png($getFilename,$getFilePath){
$medida = array('width'=>'1024','height'=>'1024',);
// Creates a temp image
$TempPngFile = imagecreatetruecolor($medida['width'], $medida['height']);
# Defines a transparent color to fill all image
$TransparentColor = imagecolorallocatealpha($TempPngFile, 0, 0, 0, 127);
imagefill($TempPngFile, 0, 0, $TransparentColor);
# Forces transparency definition
imagealphablending($TempPngFile, true);
imagesavealpha($TempPngFile, true);
# Open image
$logo = imageCreateFromPng(APPPATH.'cache/mark.png');
# Fix transparency definitions
imageAlphaBlending($logo, true);
imageSaveAlpha($logo, true);
# Open image
$img2 = imageCreateFromPng($getFilename);
# Forces transparency definition
imageAlphaBlending($img2, true);
imageSaveAlpha($img2, true);
# insert $logo and $img2 in $TempPngFile and setting positioning
imagecopy($TempPngFile, $img2, 0, 0, 0, 0, imagesx($img2), imagesy($img2));
imagecopy($TempPngFile, $logo, 25, 25, 0, 0, imagesx($logo), imagesy($logo));
# Save final image to $getFilePath
imagepng($TempPngFile, $getFilePath);
// Destroy images
imageDestroy($TempPngFile);
imageDestroy($logo);
imageDestroy($img2);
}
# Loop in $dir, get all PNG files to overlap $logo on left top
foreach ($iteractor as $entry) {
if ($entry->isFile()) {
if (in_array($entry->getExtension(), $ext)) {
$getFilename = $dir.$entry->getFilename();
$getImageName = $entry->getFilename().'_'.$i++.'_.png';
$getFilePath = $dirUpload.$getImageName;
save_png($getFilename, $getFilePath);
echo 'Created image: '.$getImageName.'<br>';
}
}
}
}
OBS: It uses the php-gd extension. And certainlly there is a way to convert JPG to PNG before overlap the files (or you should first convert your 3000 photos to PNG), but I'm too lazy now, and it works! And now it's on your hands!

Related

How to resize an uploded image before using it in an imagecreatefromjpeg function?

I have a code that allows file upload and some text input. It uses the uploaded file in an imagacreatefromjepg and imagecopymerge. I want to resize the uploaded file to a definite size which is 255x175. how can i make it? Here is what is have:
$now = time();
while(file_exists($uploadFilename = $uploadsDirectory.$now.'-'.$_FILES[$fieldname]['name']))
{
$now++;
}
// now let's move the file to its final and allocate it with the new filename
#move_uploaded_file($_FILES[$fieldname]['tmp_name'], $uploadFilename)
or error('receiving directory insuffiecient permission', $uploadForm);
$upload = $uploadFilename;
$im = imagecreatefromjpeg("bg.jpg");
$img2 = imagecreatefromjpeg($upload);
$black = imagecolorallocate($im, 0, 0, 0);
$font = 'arialbi.ttf';
$font2 = 'ariali.ttf';
imagettftext($im, 24, 0, 50, 280, $black, $font, $title);
imagettftext($im, 10, 0, 320, 362, $black, $font, $namehere);
imagecopymerge($im, $img2, 30, 350, 0, 0, imagesx($img2), imagesy($img2), 100);
$date_created = date("YmdHis");//get date created
$img_name = "-img_entry.jpg"; //the file name of the generated image
$img_newname = $date_created . $img_name; //datecreated+name
$img_dir =dirname($_SERVER['SCRIPT_FILENAME']) ."/". $img_newname; //the location to save the image
imagejpeg($im, $img_dir , 80); //function to save the image with the name and quality
$newpath = "/home3/site/public_html/mainfolder/image_entry/";//path to another folder
$newdir = $newpath.$img_newname;
copy ($img_dir, $newdir); //copy to new folder
imagedestroy($im);
Hope you can help me fix this. I have raed the posts Resize image before uploading PHP and Php resize width fix. But I dont know how to apply it in my case. Thanks for any and all help.
just after: imagecopymerge($im,... line
$mini_width = ...;// calculate desirable width
$mini_height = ...;// calculate desirable height
$mini_img = #ImageCreateTrueColor($mini_width, $mini_height);
imagecopyresampled($mini_img, $img2, 0, 0, 0, 0, $mini_width, $mini_height, imagesx($img2), imagesy($img2));
next line is $date_created = date(...
change imagejpeg($im, $img_dir , 80); to:
imagejpeg($mini_img, $img_dir , 80);
and finally change imagedestroy($im); to imagedestroy($mini_img);
Actually, you can, but not have to call imagedestroy() for all the resource images created above.

Image create using gd

I am creating image using GD library all the functions are working fine. But the main problem where i stucked that i want to merge png image over an other image but after overlapping it cannot merge properly and looking like jpg or other instead of png. I cannot upload my image here due to low reputation so click on these links below to see the image.
The image which i want to merge is this
Png image
The image where i merge above image is:
My code is here:
<?php
$im = imagecreate(288,288);
$background_color = imagecolorallocate($im, 230, 248, 248);
$file = 'images/smiley/smile'.$_POST['smiley'].'.png';
$bg = imagecreatefrompng($file);
imagealphablending($im, true);
imagesavealpha($bg, true);
imagecopyresampled($im, $bg, 80, 80, 0, 0, 50, 50, 185, 185);
header("Content-Type: image/png");
$filename = $_SESSION['rand'].'.png';
imagepng($im,$filename);
echo '<img src="'.$filename.'" alt="" />';
?>
Your background image doesn't have an alpha channel. This makes the PHP GD library do all of it's copying operations without using an alpha channel, instead just setting each pixel to be fully opaque or transparent, which is not what you want.
The simplest solution to this is to create a new image of the same size as the background that has an alpha channel, and then copy both the background and face into that one.
$baseImage = imagecreatefrompng("../../var/tmp/background.png");
$topImage = imagecreatefrompng("../../var/tmp/face.png");
// Get image dimensions
$baseWidth = imagesx($baseImage);
$baseHeight = imagesy($baseImage);
$topWidth = imagesx($topImage);
$topHeight = imagesy($topImage);
//Create a new image
$imageOut = imagecreatetruecolor($baseWidth, $baseHeight);
//Make the new image definitely have an alpha channel
$backgroundColor = imagecolorallocatealpha($imageOut, 0, 0, 0, 127);
imagefill($imageOut, 0, 0, $backgroundColor);
imagecopy($imageOut, $baseImage, 0, 0, 0, 0, $baseWidth, $baseHeight); //have to play with these
imagecopy($imageOut, $topImage, 0, 0, 0, 0, $topWidth, $topHeight); //have to play with these
//header('Content-Type: image/png');
imagePng($imageOut, "../../var/tmp/output.png");
That code produces this image:

imagecopyresized color jpeg doesn't work

Hey guys i have a problem with merging two pictures...
I am trying to merge an png file (called badge) with an useruploaded picture.
Everything works fine when the user upload a png oder gif file, but if he uploads a jpeg image the output image looks really weird. It seems it is an color problem.
Here my code:
//Calculate position for badge (right bottom corner)
$badgeRightPosition = $imageWidth - $badgeWidth;
$badgeLeftPosition = $imageHeight - $badgeHeight;
$image = imagecreatefromstring(file_get_contents($image));
$badge = imagecreatefromstring(file_get_contents($badge));
$trueColorImage = imagecreatetruecolor($imageWidth, $imageHeight);
imagealphablending($trueColorImage, true);
imagesavealpha($trueColorImage, true);
imagealphablending($badge, true);
imagesavealpha($badge, true);
imagealphablending($image, true);
imagesavealpha($image, true);
imagecopyresized($trueColorImage, $image, 0, 0, 0, 0, $imageWidth, $imageHeight, $imageWidth, $imageHeight);
imagecopyresized($trueColorImage, $badge, $badgeRightPosition, $badgeLeftPosition, 0, 0, $imageWidth, $imageHeight, $badgeWidth, $badgeHeight);
Instead of imagecreatefromstring(file_get_contents($image)); I would try to directly open the file
$imgsrc = #ImageCreateFromJPEG($image);
if (!$imgsrc) $imgsrc = #ImageCreateFromPNG($image);
if (!$imgsrc) $imgsrc = #ImageCreateFromGIF($image);
if (!$imgsrc) $imgsrc = #ImageCreateFromWBMP($image);
Or check the file type by its ending and then use one of the functions above according to the file ending.

Merge two images in php

I have two images I would like to merge then save to a new location.
I would like the second image to be place directly below the first image.
I have the following so for but the image doesn't even save.
$destimg = imagecreatefromjpeg('images/myimg.jpg');
$src = imagecreatefromgif('images/second.gif');
// Copy and merge
imagecopymerge($destimg, $src, 316, 100, 0, 0, 316, 100, 100);
Both images have a width or 316px X 100px
From the above code the $destimg should now be 316x200 but that doesn't happen. Also like it to be a new image and save to another folder.
Thanks for any help.
The best approach for this situation may be to create a new image in memory with the combined dimensions you desire, then copy or resample the existing images to the new image, and then save the new image to disk.
For example:
function merge($filename_x, $filename_y, $filename_result) {
// Get dimensions for specified images
list($width_x, $height_x) = getimagesize($filename_x);
list($width_y, $height_y) = getimagesize($filename_y);
// Create new image with desired dimensions
$image = imagecreatetruecolor($width_x + $width_y, $height_x);
// Load images and then copy to destination image
$image_x = imagecreatefromjpeg($filename_x);
$image_y = imagecreatefromgif($filename_y);
imagecopy($image, $image_x, 0, 0, 0, 0, $width_x, $height_x);
imagecopy($image, $image_y, $width_x, 0, 0, 0, $width_y, $height_y);
// Save the resulting image to disk (as JPEG)
imagejpeg($image, $filename_result);
// Clean up
imagedestroy($image);
imagedestroy($image_x);
imagedestroy($image_y);
}
Example:
merge('images/myimg.jpg', 'images/second.gif', 'images/merged.jpg');
i would like to add one more thing here if you are using PHP GD Library,then you should include imagesavealpha() and alphablending() also.
I suggest you to use Image Magick instead (pecl-imagick module or running it as command via shell). I have several reasons:
Imagick is:
faster
knows more format
makes better quality images
have more ability (for example text rotation)
and more...
Your method is Imagick::compositeImage if you use the php module. Manual: http://php.net/manual/en/function.imagick-compositeimage.php
I found the answer, use GD:
function merge($filename_x, $filename_y, $filename_result) {
// Get dimensions for specified images
list($width_x, $height_x) = getimagesize($filename_x);
list($width_y, $height_y) = getimagesize($filename_y);
// Create new image with desired dimensions
$image = imagecreatetruecolor($width_x, $height_x);
// Load images and then copy to destination image
$image_x = imagecreatefromjpeg($filename_x);
$image_y = imagecreatefromgif($filename_y);
imagecopy($image, $image_x, 0, 0, 0, 0, $width_x, $height_x);
// top, left, border,border
imagecopy($image, $image_y, 100, 3100, 0, 0, $width_y, $height_y);
// Save the resulting image to disk (as JPEG)
imagejpeg($image, $filename_result);
// Clean up
imagedestroy($image);
imagedestroy($image_x);
imagedestroy($image_y);
}
like this:
merge('images/myimage.jpg', 'images/second.gif', 'images/merged.jpg');

Background transperancy in imagerotate()

Since last 2 days, I was trying to add transperancy to the background after rotating an image using imagerotate() PHP-GD function.
But, to my great disappointment, it's not working at all.
It's just giving out a black background behind it.
Here's my code -
$patchImageS = 'image.png'; // the image to be patched over the final bg
$patchImage = imagecreatefrompng($patchImageS); // resource of image to be patched
$patchImage = imagerotate($patchImage, 23, 0, 0);
imagepng($patchImage,'tt.png');
I tried to change the parameters being passed in function to
imagerotate($patchImage, 23, 5, 0);
imagerotate($patchImage, 23, 0, 5);
Any help would be highly appreciated.
After a number of 99% finished answers, here's the solution I've found:
// Create, or create from image, a PNG canvas
$png = imagecreatetruecolor($width, $height);
// Preserve transparency
imagesavealpha($png , true);
$pngTransparency = imagecolorallocatealpha($png , 0, 0, 0, 127);
imagefill($png , 0, 0, $pngTransparency);
// Rotate the canvas including the required transparent "color"
$png = imagerotate($png, $rotationAmount, $pngTransparency);
// Set your appropriate header
header('Content-Type: image/png');
// Render canvas to the browser
imagepng($png);
// Clean up
imagedestroy($png);
The key here is to include your imagecolorallocatealpha() in your imagerotate() call...
look for imagesavealpha() in the php-documentation - i think this is what you are looking for.
EDIT: here's an example:
$png = imagecreatefrompng('./alphachannel_example.png');
// Do required operations
$png = imagerotate($png, 23, 0, 0);
// Turn off alpha blending and set alpha flag
imagealphablending($png, false);
imagesavealpha($png, true);
// Output image to browser
header('Content-Type: image/png');
imagepng($png);
imagedestroy($png);
For anyone having problems with imagecopyresampled or imagerotate with black bars on background, I have found a code example here:
https://qna.habr.com/q/646622#answer_1417035
// get image sizes (X,Y)
$wx = imagesx($imageW);
$wy = imagesy($imageW);
// create a new image from the sizes on transparent canvas
$new = imagecreatetruecolor($wx, $wy);
$transparent = imagecolorallocatealpha($new, 0, 0, 0, 127);
$rotate = imagerotate($imageW, 280, $transparent);
imagealphablending($rotate, true);
imagesavealpha($rotate, true);
// get the newest image X and Y
$ix = imagesx($rotate);
$iy = imagesy($rotate);
//copy the image to the canvas
imagecopyresampled($destImg, $rotate, 940, 2050, 0, 0, $ix, $iy, $ix, $iy);

Categories