Jump to content

Image copy error?


bobdole

Recommended Posts

I'm using a simple script for Image Copy. It's adding an image to the bottom of another image. Both images are the same width. The script works fine, but it changes the color of my images and makes it blurry??? I have no clue why, can anyone help me out :)

 

<?php
$top_file = 'image1.jpg';
$bottom_file = 'image2.jpg';
 
$top = imagecreatefromjpeg($top_file);
$bottom = imagecreatefromjpeg($bottom_file);
 
// get current width/height
list($top_width, $top_height) = getimagesize($top_file);
list($bottom_width, $bottom_height) = getimagesize($bottom_file);
 
// compute new width/height
$new_width = ($top_width > $bottom_width) ? $top_width : $bottom_width;
$new_height = $top_height + $bottom_height;
 
// create new image and merge
$new = imagecreate($new_width, $new_height);
imagecopy($new, $top, 0, 0, 0, 0, $top_width, $top_height);
imagecopy($new, $bottom, 0, $top_height+1, 0, 0, $bottom_width, $bottom_height);
 
// save to file
imagejpeg($new, 'uploads/merged_image.jpg');
?>

 

 

 

 

Link to comment
Share on other sites

First thing I can think of without really going into your script is that imagejpeg() doesnt provide its third argument, which is quality.

By default, the quality is roughly lowered to 75%. You can set it to 100 as well.

Link to comment
Share on other sites

That's probably it! Thanks, I'm trying it out and trying to learn to use that function correctly. Although that might not be directly it. It changes the image to horrible quality where it is incredibly blurry and an entirely different color. I;m not sure if the difference between 100% and 75% would do that.

Link to comment
Share on other sites

Alright, the quality was not the problem, but it was a great guess.

 

 

I'm trying this script now. It works but the image shows up next to each other left to right instead of below the first image.

 

<?php




merge('image1.jpg', 'image2.jpg', 'uploads/merged.jpg');


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 = imagecreatefromjpeg($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);


}
?>

Any slight modifications to get this to work?  :happy-04:

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.