Related
I'm uploading images from the pc to a server using this php code
$ImageToLoad=mysql_real_escape_string($_POST['image_attached']);
if($ImageToLoad){
$token=$token;//variable
$ImageToLoad = str_replace('data:image/png;base64,', '', $ImageToLoad);
$ImageToLoad = str_replace('data:image/jpeg;base64,', '', $ImageToLoad);
$ImageToLoad = str_replace(' ', '+', $ImageToLoad);
$fileData = base64_decode($ImageToLoad);
$destino_path="/images/$token/image.png";
file_put_contents($destino_path, $fileData);
}
It works ok.
PROBLEM
I need to know how to resize it/crop them before to store the image in the server. Otherwise it keeps the same size (problem when huge image)
Resizing images can be is a costly operation and should be handled by a specialized library. In the past, the default pointer was ImageMagick's Imagick::resizeImage. However, this package does not work for everybody and other solutions have emerged in the meantime.
I suggest gumlet's php-image-resize. Resizing a base64 encoded image can be as simple as that:
$image = ImageResize::createFromString(base64_decode('R0lGODlhAQABAIAAAAQCBP///yH5BAEAAAEALAAAAAABAAEAAAICRAEAOw=='));
$image->scale(50);
$image->save('image.jpg');
If we want to create a temp image from an image file for resizing, we can use
imagecreatefromjpeg($filename);
or
imagecreatefrompng($filename);
If we want to create a temp image from blob we can use
imagecreatefromstring($blob);
imagecreatefromstring()
So, give this a try:
<?php
$filename = 'folder_name/resized_image.png'; // output file name
$im = imagecreatefromstring($fileData);
$source_width = imagesx($im);
$source_height = imagesy($im);
$ratio = $source_height / $source_width;
$new_width = 300; // assign new width to new resized image
$new_height = $ratio * 300;
$thumb = imagecreatetruecolor($new_width, $new_height);
$transparency = imagecolorallocatealpha($thumb, 255, 255, 255, 127);
imagefilledrectangle($thumb, 0, 0, $new_width, $new_height, $transparency);
imagecopyresampled($thumb, $im, 0, 0, 0, 0, $new_width, $new_height, $source_width, $source_height);
imagepng($thumb, $filename, 9);
imagedestroy($im);
?>
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.
I want to insert a signature (saved as png file) on the bottom of a letter (saved as jpg file) in a php site.
I used imagecopymerge, but it creates a black image file instead of my request.
I used this code too, but no result.
function merge($filename_x, $filename_y, $filename_result) {
list($width_x, $height_x) = getimagesize($filename_x);
list($width_y, $height_y) = getimagesize($filename_y);
$image = imagecreatetruecolor($width_x + $width_y, $height_x);
$image_x = imagecreatefromjpeg($filename_x);
$image_y = imagecreatefromgif($filename_y);
imagecopy($image, $image_x, 0, 20, 30, 50, $width_x, $height_x);
imagecopy($image, $image_y, $width_x, 0, 10, 0, $width_y, $height_y);
imagejpeg($image, $filename_result);
imagedestroy($image);
imagedestroy($image_x);
imagedestroy($image_y);
}
merge('myimg.jpeg', 'first.gif', 'merged.jpg');
Please try this function, I have customized yours.
function merge($filename_x, $filename_y, $filename_result) {
$source = imagecreatefromjpeg($filename_x);
$tobeMerged = imagecreatefromgif($filename_y);
//add signature on bottom right
imagecopymerge($source, $tobeMerged, imagesx($source) - imagesx($tobeMerged), imagesy($source) - imagesy($tobeMerged), 0, 0, imagesx($tobeMerged), imagesy($tobeMerged), 100);
//save your merged image
imagejpeg($source, $filename_result);
//destroy image resources to free memory
imagedestroy($source);
imagedestroy($tobeMerged);
}
merge('myimg.jpeg', 'first.gif', 'merged.jpg');
This function works for me. Since I have not seen your images, I can tell you what I am using to test it.
bg.jpg = 400X400 jpg
fg.gif = 200X200 gif (With transparent background)
function merge($filename_x, $filename_y, $filename_result) {
list($width_x, $height_x) = getimagesize($filename_x);
list($width_y, $height_y) = getimagesize($filename_y);
$image = imagecreatetruecolor($width_x, $height_x);
$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, 0, 0, 0, 0, $width_y, $height_y);
imagejpeg($image, $filename_result);
imagedestroy($image);
imagedestroy($image_x);
imagedestroy($image_y);
}
merge('bg.jpg', 'Untitled.gif', 'merged.jpg');
This seems to work fine. I am assuming maybe you are having some positioning issues. Try everything at starting position 0 then start moving until you get the desired effect.
Are you able to run command line tools (eg via exec)? If so, imagemagick command line tools can do just about any image manipulation you need. The layering functionality sounds like what you are after:
echo exec('composite -geometry +5+10 image1.jpg image2.png image2.png');
Your gif might have a color palette and is not a true color image. If your php version is 5+ check with imageistruecolor and in case use imagepalettetotruecolor.
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');
I'm trying to merge two images together with PHP.
For example... how would I go about placing image one on top of image two or merge, with basic PHP?
I have tried something such as watermarking, but it doesn't seem to be working.
Image One
Image Two
...and have it turn into this? FINAL RESULT:
I got it working from one I made.
<?php
$dest = imagecreatefrompng('vinyl.png');
$src = imagecreatefromjpeg('cover2.jpg');
imagealphablending($dest, false);
imagesavealpha($dest, true);
imagecopymerge($dest, $src, 10, 9, 0, 0, 181, 180, 100); //have to play with these numbers for it to work for you, etc.
header('Content-Type: image/png');
imagepng($dest);
imagedestroy($dest);
imagedestroy($src);
?>
Question is about merging two images, however in this specified case you shouldn't do that. You should put Content Image (ie. cover) into <img /> tag, and Style Image into CSS, why?
As I said the cover belongs to the content of the document, while that vinyl record and shadow are just a part of the page styles.
Such separation is much more convenient to use. User can easily copy that image. It's easier to index by web-spiders.
Finally, it's much easier to maintain.
So use a very simple code:
<div class="cover">
<img src="/content/images/covers/movin-mountains.png" alt="Moving mountains by Pneuma" width="100" height="100" />
</div>
.cover {
padding: 10px;
padding-right: 100px;
background: url(/style/images/cover-background.png) no-repeat;
}
ImageArtist is a pure gd wrapper authored by me, this enables you to do complex image manipulations insanely easy, for your question solution can be done using very few steps using this powerful library.
here is a sample code.
$img1 = new Image("./cover.jpg");
$img2 = new Image("./box.png");
$img2->merge($img1,9,9);
$img2->save("./merged.png",IMAGETYPE_PNG);
This is how my result looks like.
You can try my function for merging images horizontally or vertically without changing image ratio. just copy paste will work.
function merge($filename_x, $filename_y, $filename_result, $mergeType = 0) {
//$mergeType 0 for horizandal merge 1 for vertical merge
// Get dimensions for specified images
list($width_x, $height_x) = getimagesize($filename_x);
list($width_y, $height_y) = getimagesize($filename_y);
$lowerFileName = strtolower($filename_x);
if(substr_count($lowerFileName, '.jpg')>0 || substr_count($lowerFileName, '.jpeg')>0){
$image_x = imagecreatefromjpeg($filename_x);
}else if(substr_count($lowerFileName, '.png')>0){
$image_x = imagecreatefrompng($filename_x);
}else if(substr_count($lowerFileName, '.gif')>0){
$image_x = imagecreatefromgif($filename_x);
}
$lowerFileName = strtolower($filename_y);
if(substr_count($lowerFileName, '.jpg')>0 || substr_count($lowerFileName, '.jpeg')>0){
$image_y = imagecreatefromjpeg($filename_y);
}else if(substr_count($lowerFileName, '.png')>0){
$image_y = imagecreatefrompng($filename_y);
}else if(substr_count($lowerFileName, '.gif')>0){
$image_y = imagecreatefromgif($filename_y);
}
if($mergeType==0){
//for horizandal merge
if($height_y<$height_x){
$new_height = $height_y;
$new_x_height = $new_height;
$precentageReduced = ($height_x - $new_height)/($height_x/100);
$new_x_width = ceil($width_x - (($width_x/100) * $precentageReduced));
$tmp = imagecreatetruecolor($new_x_width, $new_x_height);
imagecopyresampled($tmp, $image_x, 0, 0, 0, 0, $new_x_width, $new_x_height, $width_x, $height_x);
$image_x = $tmp;
$height_x = $new_x_height;
$width_x = $new_x_width;
}else{
$new_height = $height_x;
$new_y_height = $new_height;
$precentageReduced = ($height_y - $new_height)/($height_y/100);
$new_y_width = ceil($width_y - (($width_y/100) * $precentageReduced));
$tmp = imagecreatetruecolor($new_y_width, $new_y_height);
imagecopyresampled($tmp, $image_y, 0, 0, 0, 0, $new_y_width, $new_y_height, $width_y, $height_y);
$image_y = $tmp;
$height_y = $new_y_height;
$width_y = $new_y_width;
}
$new_width = $width_x + $width_y;
$image = imagecreatetruecolor($new_width, $new_height);
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);
}else{
//for verical merge
if($width_y<$width_x){
$new_width = $width_y;
$new_x_width = $new_width;
$precentageReduced = ($width_x - $new_width)/($width_x/100);
$new_x_height = ceil($height_x - (($height_x/100) * $precentageReduced));
$tmp = imagecreatetruecolor($new_x_width, $new_x_height);
imagecopyresampled($tmp, $image_x, 0, 0, 0, 0, $new_x_width, $new_x_height, $width_x, $height_x);
$image_x = $tmp;
$width_x = $new_x_width;
$height_x = $new_x_height;
}else{
$new_width = $width_x;
$new_y_width = $new_width;
$precentageReduced = ($width_y - $new_width)/($width_y/100);
$new_y_height = ceil($height_y - (($height_y/100) * $precentageReduced));
$tmp = imagecreatetruecolor($new_y_width, $new_y_height);
imagecopyresampled($tmp, $image_y, 0, 0, 0, 0, $new_y_width, $new_y_height, $width_y, $height_y);
$image_y = $tmp;
$width_y = $new_y_width;
$height_y = $new_y_height;
}
$new_height = $height_x + $height_y;
$image = imagecreatetruecolor($new_width, $new_height);
imagecopy($image, $image_x, 0, 0, 0, 0, $width_x, $height_x);
imagecopy($image, $image_y, 0, $height_x, 0, 0, $width_y, $height_y);
}
$lowerFileName = strtolower($filename_result);
if(substr_count($lowerFileName, '.jpg')>0 || substr_count($lowerFileName, '.jpeg')>0){
imagejpeg($image, $filename_result);
}else if(substr_count($lowerFileName, '.png')>0){
imagepng($image, $filename_result);
}else if(substr_count($lowerFileName, '.gif')>0){
imagegif($image, $filename_result);
}
// Clean up
imagedestroy($image);
imagedestroy($image_x);
imagedestroy($image_y);
}
merge('images/h_large.jpg', 'images/v_large.jpg', 'images/merged_har.jpg',0); //merge horizontally
merge('images/h_large.jpg', 'images/v_large.jpg', 'images/merged.jpg',1); //merge vertically
Use the GD library or ImageMagick. I googled 'PHP GD merge images' and got several articles on doing this. In the past what I've done is create a large blank image, and then used imagecopymerge() to paste those images into my original blank one. Check out the articles on google you'll find some source code you can start using right away.
You can do this with the ImageMagick extension. I'm guessing that the combineImages() method will do what you want.
Merger two image png and jpg/png [Image Masking]
//URL or Local path
$src_url = '1.png';
$dest_url = '2.jpg';
$src = imagecreatefrompng($src_url);
$dest1 = imagecreatefromjpeg($dest_url);
//if you want to make same size
list($width, $height) = getimagesize($dest_url);
list($newWidth, $newHeight) = getimagesize($src_url);
$dest = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresampled($dest, $dest1, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
list($src_w, $src_h) = getimagesize($src_url);
//merger with same size
$this->imagecopymerge_alpha($dest, $src, 0, 0, 0, 0, $src_w, $src_h, 100);
//show output on browser
header('Content-Type: image/png');
imagejpeg($dest);
imagecopymerge_alpha
function imagecopymerge_alpha($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $pct)
{
$cut = imagecreatetruecolor($src_w, $src_h);
imagecopy($cut, $dst_im, 0, 0, $dst_x, $dst_y, $src_w, $src_h);
imagecopy($cut, $src_im, 0, 0, $src_x, $src_y, $src_w, $src_h);
imagecopymerge($dst_im, $cut, $dst_x, $dst_y, 0, 0, $src_w, $src_h, $pct);
}
The GD Image Manipulation Library in PHP is probably the best for working with images in PHP. Try one of the imagecopy functions (imagecopy, imagecopymerge, ...). Each of them combine 2 images in different ways. See the php documentation on imagecopy for more information.