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.
Related
Very new to php and im trying to merge two images together using GD.
I have tried doing this, however it is causing the merged image to have a weird black background.
Anyone know where i am going wrong?
<?php
$image_1 = imagecreatefrompng('image.png');
$overlay = imagecreatefrompng('image2.png');
$size = getimagesize('image2.png');
imagecopy($image_1, $overlay, 0, 0, 0, 0, $size[0], $size[1]);
imagepng($image_1, "mergedImage.png");
?>
<img src="image.png"/>
<img src="image2.png"/>
<img src="mergedImage.png">
This is the output i get.
source images:
Telling GD to retain alpha channel information immediately after creating your target image should fix the issue:
<?php
$image_1 = imagecreatefrompng('image.png');
imagesavealpha($image_1, true);
$overlay = ...
At the moment you end up with a duplicate of image2.png, but I assume you want to achieve a different result:
I'm trying to crop then resize an image on PHP v5.4, I've read these resources
Put PNG over a JPG in PHP
http://php.net/manual/en/function.imagecopy.php
http://php.net/manual/en/function.imagecopyresampled.php
http://php.net/manual/en/function.call-user-func-array.phPP
PHP watermarking
http://php.net/manual/en/function.imagecreatetruecolor.php
My code is based off the answer from Cropping image in PHP (the dimensions between these images vary alot).
I want to resize this image from 1151x768 to 200x82 and crop the background section at x: 0, y: 686
I'd prefer not bloating the question with the entire 600 lines in this question, $output refers to setwidth1200nzpioneerthursday08398 image
<?php
$output = imagecreatefromjpeg("setwidth1200nzpioneerthursday08398.jpg");
$source_crop_image = imagecreatetruecolor(200, 82);
if(!is_resource($source_crop_image)) {
return $source_crop_image;
}
imagealphablending($output, true);
$source_copy_result = imagecopy($output, $source_crop_image, 0, 0, 0, 686, 200, 82);
$source_copy_result = (bool) $source_copy_result;
if(!$source_copy_result) {
return false;
}
$source_image_result = imagejpeg($source_crop_image, "images/mynewimage.jpg");
$source_image_result = (bool) $source_image_result;
?>
My Image setwidth1200nzpioneerthursday08398
Ideally I'm trying to get it crop the RED SECTION, while keeping the scale intact then resizing to 200x82
My Result
My Expected Result (I created this image using GIMP).
I have no idea why my resulting image is a black box ..
You have imagecopy() arguments in wrong order.
The right one is $source_copy_result = imagecopy($source_crop_image, $output, 0, 0, 0, 686, 200, 82);
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.
I have the following PHP code...
$destination_image_x = "235";
$destination_image_y = "230";
$destination_image = imagecreatetruecolor($destination_image_x, $destination_image_y);
$source_image_x = imagesx($temp_profile_picture_converted);
$source_image_y = imagesy($temp_profile_picture_converted);
$temp_profile_picture_converted = imagecopyresampled($destination_image, $temp_profile_picture_converted, 0, 0, 0, 0, $destination_image_x, $destination_image_y, $source_image_x, $source_image_y);
imagejpeg($temp_profile_picture_converted, $user_profile_picture_filename,'75');
imagedestroy($temp_profile_picture_converted);
The function of this code is to scale an image passed to it, and save it at a specified directory. I'm able to save the image using "imagejpeg" normally if I ommit the resizing snippet. The variable "$temp_profile_picture_converted" is assigned to a jpg image I created from the user's uploaded image with "imagecreatefromjpeg." (Or imagecreatefrompng, or imagecreatefromgif, etc.)
You are using the same variable $temp_profile_picture_converted twice in the following line. The function imagecopyresampled() returns a boolean and is overwriting the image this variable holds. The return value from this function is only to check success. Change it to:
if (! imagecopyresampled($destination_image, $temp_profile_picture_converted, 0, 0, 0, 0, $destination_image_x, $destination_image_y, $source_image_x, $source_image_y)){
// then give error message...
}
UPDATE
However, you have other errors. You need to change the first parameter of imagejpeg(). I also changed the size vars from strings to numbers - not sure if it mattered.
imagejpeg($destination_image, $user_profile_picture_filename,75);
I successfully ran the following code
$destination_image_x = 235;
$destination_image_y = 230;
$source_image_x = imagesx($temp_profile_picture_converted);
$source_image_y = imagesy($temp_profile_picture_converted);
$destination_image = imagecreatetruecolor($destination_image_x, $destination_image_y);
imagecopyresampled($destination_image, $temp_profile_picture_converted, 0, 0, 0, 0, $destination_image_x, $destination_image_y, $source_image_x, $source_image_y);
imagejpeg($destination_image, $user_profile_picture_filename,75);
imagedestroy($temp_profile_picture_converted);
imagedestroy($destination_image);
Note that I also added the last statement imagedestroy($destination_image);
I am attempting to merge two png images by placing a smaller png behind an image with a "hole" in the center with transparency.
The "Front" image is $src in this example
The "Back" image is $dest in the example
So far, i've gotten it to work in reverse (by putting the $dest image / smaller image in front) using the following code:
imagecopymerge($src, $dest, 300, 150, 0, 0, 150, 150, 100);
However, i'm not sure how to do it with the smaller image "Behind" the bigger image so that it fits perfectly in the hole.
Do I need to recreate the image ($dest) as a larger image (500 x 500) to "paste" the $src image over top of with 0 offset? This stuff is confusing :S
Figured it out.
First I merged the smaller image onto a blank image below that matched the larger image.
Then, I merged the image with the hole onto the new image created above. See as follows:
// Get size of larger image
$sz = getimagesize("larger.jpg");
// Create resources
$backing = imagecreatetruecolor($sz[0],$sz[1]);
$img1 = imagecreatefrompng("larger.jpg");
$img2 = imagecreatefrompng("smaller.jpg");
// Merge backing
imagecopymerge($backing, $img2, 300, 150, 0, 0, 150, 150, 100);
// Merge main
imagecopymerge($backing,$img1, 0, 0, 0, 0, $sz[0], $sz[1], 100);
// Save new image
imagepng($backing,$save);
// Destroy resources
imagedestroy($backing);
imagedestroy($img1);
imagedestroy($img2);
Hope this helps someone!