PHP set the attribute for an image with matrix values - php

as the topic,I need to set the attribute for an image or create an image with the attribute what I get from a client as the matrix values.
I found a function named imageconvolution,but it doesn't work out.Maybe I used it incorrectly.
here is the code:
<?php
$image = imagecreatefromgif('http://www.php.net/images/php.gif');
$emboss = array(array(0, 0, 100), array(0, 0, 200), array(0, 0, 1));
imageconvolution($image, $emboss, 1, 0);
header('Content-Type: image/png');
imagepng($image, null, null);
?>
the matrix values are used to scale or rotate or move the image.Is these code right?
I hope to find out someone to teach me.
Thanks a lot.

Scaling, rotation and moving are affine transforms. You cannot use convolution for this matrixes.
I think the easiest way to use the php Imagick extension. It has an affineTransformImage function which you can use: http://php.net/manual/en/imagick.affinetransformimage.php

Related

How to make a transparent image in PHP

I want to remove a background from my image:
My script can be found here:
<?php
//header('Content-type:jpeg');
// Create image instances
$dest = imagecreatefromjpeg('images/bg.jpg');
$src = imagecreatefrompng('images/Title.png');
imagealphablending($src, false);
imagesavealpha($src, true);
// Copy and merge
imagecopymerge($dest, $src, 0, 300, 0, 0, 700, 150, 75);
// Output and free from memory
imagejpeg($dest,'images/print/imagecopymerge.jpg');
imagedestroy($dest);
imagedestroy($src);
?>
<img src="images/print/imagecopymerge.jpg">
Can anyone help me accomplish this?
It might be easier to make a png image and use that with transparency (Could use Paint.net free :) or Photoshop/Illustrator Paid :(. This will give you more time to work on other things that might you might be finding hard to work out.

Convert image to sketch using php imagick

Trying to achieve sketch effect using php, but unable to get desired output.
Tried with
$im1->sketchimage(2, 1, -20);
but getting blur sort of image only.
Also, looked out and found
*s = Read-File-Into-Image("/path/to/image")
*g = ConvertToGrayScale(s)
*i = Invert Colors(g)
*b = ApplyGaussianBlur(i)
*result = Colour Dodge Blend Merge(b,g)
and tried
$im1->edgeImage(2);
$im1->contrastStretchImage(30, 500);
$im2 = $im1;
$im1->modulateImage(100, 20, 50);
$im1->negateImage(FALSE);
$im1->gaussianBlurImage(5, 1, FALSE);
$im1->compositeImage($im2, imagick::COMPOSITE_COLORDODGE, 0, 0 );
still not getting the desired output.
the radius should be larger than sigma. Else make it 0, so that sdk takes care. use a good quality image and format.
For reference..http://php.net/manual/en/imagick.sketchimage.php

Resize with imagecopyresampled and imagejpeg not working

I'm trying to resize a picture using the following PHP script.
$tn = imagecreatetruecolor(1836, 3264);
$newImage = imagecreatefromjpeg('user/354010050076877/2.jpg');
imagecopyresampled($tn, $newImage, 0, 0, 0, 0, 1836, 3264, 739, 1162);
imagejpeg($tn, 'MyFile.jpg');
The image is created at MyFile.jpg but it's still the original size.
I also tried replacing line 4 with ...
file_put_contents('MyFile.jpg', $tn);
When I try that it returns
"Warning: file_put_contents(): supplied resource is not a valid stream resource in /home/content/01/7258201/html/imgTools/resize.php on line 6"
What do I need to change in my script to get image resize working?
EDIT:
I had mixed the order of values on 'imagecopyresampled' however even after switching them It's not really resizing correctly so I'm still looking for a good fix for this. See my own answer for more details.
Try this:
$tn = imagecreatetruecolor(739, 1162); // the first line in your script
I tested your script (that one using imagejpeg) and it works on my end. So probably something in your GD library configuration/setting ...
Problem was I had put the wrong values for new width and height and mixed them with the old changed.
imagecopyresampled($tn, $newImage, 0, 0, 0, 0, 1836, 3264, 739, 1162);
to
imagecopyresampled($tn, $newImage, 0, 0, 0, 0, 739, 1162, 1836, 3264);
Thought it's still not completely working as it resizes the old image onto a larger black space.

How can I merge 3 images into 1 image via PHP?

I really cannot find a way to successfully do it.. I've searched google for this and it either has black shades around the images or all the images don't overlap. Could you please help?
I am alright at PHP; I'd give myself a 2/5.. I would really appreciate if someone would be willing to help me out.
I'm looking for a simple api that goes something like:
$color=$_GET['color'];
$face=$_GET['face'];
$hat=$_GET['hat'];
echo '<img src="avatar.php?color=$color&face=$face&hat=$hat">';
Thanks for any help in advance. I can understand php from my knowledge of other languages, too, so don't be afraid to talk technical with me; but not too technical.
there are so many comments on this answer so I'm posting this as an answer.
Got it working on my pc.
use svens code :
$images = array( $_GET['color'], $_GET['face'], $_GET['hat'] );
// Allocate new image
$img = imagecreatetruecolor(58, 75);
// Make alpha channels work
imagealphablending($img, true);
imagesavealpha($img, true);
foreach($images as $fn) {
// Load image
$cur = imagecreatefrompng($fn);
imagealphablending($cur, true);
imagesavealpha($cur, true);
// Copy over image
imagecopy($img, $cur, 0, 0, 0, 0, 58, 75);
// Free memory
imagedestroy($cur);
}
header('Content-Type: image/png'); // Comment out this line to see PHP errors
imagepng($img);
?>
I renamed your images like this so its easier :
smile : a.png
headset : b.png
blue : c.png
Turns out the problem is with the layering it. Putting one behind the other
after you rename the images, use this url -- it will work(works on my pc).
YOUR_FILE.php?hat=b.png&color=c.png&face=a.png
This will still give you a black background. I am not sure if you have the exact same code as above in your file on the server - because I played around with the image order on your link and it does not help. Try copy-pasting this exact same code on a different file and then trying. Play around with the order and check the results.
Here's some code to get you started. However you should note that image processing with gd and alpha channels is voodoo.
<?php
$images = array( $_GET['color'], $_GET['face'], $_GET['hat'] );
// Allocate new image
$img = imagecreatetruecolor(58, 75);
// Make alpha channels work
imagealphablending($img, true);
imagesavealpha($img, true);
foreach($images as $fn) {
// Load image
$cur = imagecreatefrompng($fn);
imagealphablending($cur, true);
imagesavealpha($cur, true);
// Copy over image
imagecopy($img, $cur, 0, 0, 0, 0, 58, 75);
// Free memory
imagedestroy($cur);
}
header('Content-Type: image/png'); // Comment out this line to see PHP errors
imagepng($img);
?>
What you still have to do now is checking the return values (look up the image* functions in the manual) to make sure it doesn't fail silently.
I can't really promise it's going to work with the alpha channels.. If not you'll probably have to go through the comments to the imagecopymerge() or imagecopy() on php.net and see if I missed something.

PNG composition using GD and PHP

I am trying to take a rectangular png and add depth using GD by duplicating the background and moving it down 1 pixel and right 1 pixel. I am trying to preserve a transparent background as well.
I am having a bunch of trouble with preserving the transparency.
Any help would be greatly appreciated.
Thanks!
$obj = imagecreatefrompng('rectangle.png');
$depth = 5;
$obj_width = imagesx($obj);
$obj_height = imagesy($obj);
imagesavealpha($obj, true);
for($i=1;$i<=$depth;$i++){
$layer = imagecreatefrompng('rectangle.png');
imagealphablending( $layer, false );
imagesavealpha($layer, true);
$new_obj = imagecreatetruecolor($obj_width+$i,$obj_height+$i);
$new_obj_width = imagesx($new_obj);
$new_obj_height = imagesy($new_obj);
imagealphablending( $new_obj, false );
imagesavealpha($new_obj, true);
$trans_color = imagecolorallocatealpha($new_obj, 0, 0, 0, 127);
imagefill($new_obj, 0, 0, $trans_color);
imagecopyresampled($new_obj, $layer, $i, $i, 0, 0, $obj_width, $obj_height, $obj_width, $obj_height);
//imagesavealpha($new_obj, true);
//imagesavealpha($obj, true);
}
header ("Content-type: image/png");
imagepng($new_obj);
imagedestroy($new_obj);
The solution to virtually any related problem with PHP GD is using this small utility:
http://phpimageworkshop.com/
It´s a PHP Class based on GD... but it´s has two "insignificant" differences:
Extremely easy
Always get the work done
Two days ago I have a similar problem (Join multiple PNG Images into a single one PNG using PHP) but this Library saves the day!

Categories