We are processing nearly 20M existing images (not files), all of which have been converted to the raw PNG data. Each image is always 640x480, and we want them all to be saved as square 640x640 files, with the original image on the top of the larger canvas, leaving the additional lower 160px for descriptive text to be added.
Our PNG data being retrieved is like this:
data:image/png;base64,iVBORw0 ... kJggg==
I've seen lots of posts and examples showing work with resizing, transparencies, colors, etc., but cannot determine where to start creating a new empty 640x640 "canvas" with existing PNG data.
If this is a duplicate question, I'd be happy to delete it if I can get some direction on how to begin. Without knowing the basics to get started, googling things like "image create png canvas" hasn't been helpful. Clearly, image processing is new to me, so apologize if I'm not being clear enough. Thanks in advance.
This should work for you:
$imgData = 'data:image/png;base64,iVBORw0 ... kJggg==';
$img = imagecreatefromstring(base64_decode(preg_replace('#^data:image/\w+;base64,#i', '', $imgData)));
$targetImage = imagecreatetruecolor(640, 640);
imagecopyresampled($targetImage, $img, 0, 0, 0, 0, 640, 480, 640, 480);
imagepng($targetImage, $yourPath);
Of course you might want to add some checks, background color etc. but I think you will be ready to start with this.
Related
So here's what I'm trying to do, basically when the users upload a new image, I want to make an image half that size (keeping the proportion) and half the resolution, but save both versions. Maybe save the original as 'image-upload.jpg' and the one I modify using php save it as 'image-upload-halved.jpg'
I've messed around with wordpress filters, but can't seem to get it. Below is along the lines of what I was thinking I should do, but I really have no idea.
add_filter('wp_handle_upload_prefilter', 'custom_upload_filter' );
function custom_upload_filter( $file ){
// here I was hoping I could do the image manipulation
// and also save both versions of the image
}
Any advice or links to other wordpress filters that might fit the job better would be awesome, too.
Thanks!
Take a look at the documentation for add_image_size https://developer.wordpress.org/reference/functions/add_image_size/
You should be able to add a new image size like this:
add_image_size( 'custom-size', 220, 180 ); // 220 pixels wide by 180 pixels tall, soft proportional crop mode
Replace "custom-size" with a name for your size and the pixel values that you want.
You can call the image in your template like this:
// Assuming your Media Library image has a post id of 42...
echo wp_get_attachment_image( 42, 'your-custom-size' );
so i have an image that is originally 1696x1696. i want to use a php file to MIME a png file. what i want the php to do is crop the original file and produce a quarter of the image. Later i plan to use $_GET variables to return which quadrant i want, but for testing/debugging, im just aiming to get the top left quadrant.
here's the code:
Header("Content-type:image/png");
$newImg =imagecreatefrompng('test.png');
//manually entered half height and width
$img=imagecreatetruecolor(848,848);
//here is where the bugs keep flawing the image
imagecopyresampled($img,$newImg,0,0,0,0,1696,1696,1696,1696);
imagepng($img);
imagedestroy($img);
this will produce the image (top, left) like it's supposed to, however it adds several smaller resampled images on top of it. no matter how i toy with it, i cant get it right. i've also tried imagecopy() and cant get it right as well. looked up tutorials and i cant seem to find on that helps.
Your code looks fine. The only thing I would change is to use imagecopyresized() instead of imagecopyresampled() in this use case.
Header("Content-type:image/png");
$source = imagecreatefrompng('images/test.png');
// manually entered half height and width
$thumb = imagecreatetruecolor(848,848);
imagecopyresized($thumb, $source, 0, 0, 0, 0, 1696, 1696, 1696, 1696);
imagepng($thumb);
imagedestroy($thumb);
I am guessing that earlier in your tests, you were overwriting your original image. That would explain the...
however it adds several smaller resampled images on top of it...
...part of your experience. Each time you ran the code, you picked up the previously modified file.
ok, so after enough headbanging and hair-tearing out, i decided to just go back to photoshop and overwrite the .png with my original .psd. starting to get somewhere now. i got my quadrant without all the ridiculousness. when i get a better understanding, i might come back and explain where i kept going wrong
I have a script that handles/scales uploaded images. I noticed that some of the images come out significantly darkened, and through a process of elimination tracked the darkening back to this section of code:
$scaled = new IMagick();
$scaled->newPseudoImage($original->getImageWidth(), $original->getImageHeight(), 'xc:white');
$scaled->compositeImage($original, Imagick::COMPOSITE_DEFAULT, 0, 0);
$scaled->flattenImages();
What I'm doing here is trying to eliminate issues with transparent backgrounds in some images coming through as black when I convert to jpg.
Does anyone have any idea which part of this code is darkening the image and what a good way to fix it might be?
Edit: Still haven't figured out the heart of this issue, but I did find that I can avoid doing this to images that don't need it by wrapping it in:
if($original->getImageAlphaChannel()){
I have the last few hours tried to get a PNG logo with a transparent background on top of a JPG background. I have tried several ways and with several globals as well, but I do not seem to be able to get the result I want.
"First Attempt":
$overlay = new Imagick('overlay.png');
$image = new Imagick('background.jpg');
$image->compositeImage($overlay, Imagick::COMPOSITE_DEFAULT, 10, 10);
$image->writeImage('background.jpg'); //replace original background
$overlay->destroy();
$image->destroy();
As you can see, the Jaguar logo is all black.
"Second Attempt"
$overlay = new Imagick('overlay.png');
$image = new Imagick('background.jpg');
$image->setImageColorspace($overlay->getImageColorspace() );
$image->compositeImage($overlay, Imagick::COMPOSITE_DEFAULT, 10, 10);
$image->writeImage('background.jpg'); //replace original background
$overlay->destroy();
$image->destroy();
This one the Jaguar logo looks like it should, but the background is all messed up now.
I have tried with Imagick::setImageMatte and tried to add the overlay to a white background (thought I does need to have a transparent background) and still it won't display the image properly. I have tried many other variations of the 2 above snippets but they all seem to make the PNG completely or partial black.
What am I missing or doing wrong? Can anyone give me a nudge in the right direction?
Please note this needs to be done in PHP.
Thank you very much!
I am a huge idiot! Turns out I forgot to convert the images from CMYK to RGB.
For anyone who might encounter this in the future, learn from my incompetence!
I was trying to overlay a png with transparency over the top of another png. I used this line from the PHP docs.
$src1->compositeImage($src2, Imagick::COMPOSITE_MATHEMATICS, 0, 0);
but I was getting the same problem. The overlay came through as black only. Changing it to this seemed to fix the colours.
$src1->compositeImage($src2, Imagick::COMPOSITE_DEFAULT, 0, 0);
I am searching for a way to overlay an image on an existing image.
e.g:
+
I have found a great example over here: PNG overlay using one single Image element.
but I have two problems with these.
First of all, I don't want the dimensions to be equal to each other. e.g (215*215 on 215*215). This is because my users would have the ability to choose where they want to put their image. (Top, left, bottom, top-right) so 8 directions.
The second problem is that in that example, only 2 images are allowed to overlay. My users (again) will have the ability to put multiple images on top of it.
I have a little knowledge of Javascript and PHP, so it would be great if you guys (and girls) could help me out.
Sincerely,
You can do this using GD library. There is function to "merge" images called imagecopymerge.
Here is a very simple example how to merge images:
<?php
header('Content-Type: image/jpeg');
$bg = imagecreatefromjpeg('background.jpg');
$img = imagecreatefromjpeg('image.jpg');
imagecopymerge($bg, $img, 0, 0, 0, 0, imagesx($bg), imagesy($bg), 75);
imagejpeg($bg, null, 100);
?>