By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,466 Members | 1,202 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,466 IT Pros & Developers. It's quick & easy.

Copy Image

P: 13
Hello,I put the coding below.Can anyone help me why its not working.


Expand|Select|Wrap|Line Numbers
  1. <?php
  2. header("content-type:image/jpg");
  3.  $img=imagecreatefromjpeg("Winter.jpg");
  4. //Convert $img to truecolor
  5. echo $w = imagesx($img);
  6. echo $h = imagesy($img);
  7.  
  8. if (!imageistruecolor($img)) {
  9.    $original_transparency = imagecolortransparent($img);
  10.   //we have a transparent color
  11.   if ($original_transparency >= 0) {
  12.     //get the actual transparent color
  13.     $rgb = imagecolorsforindex($img, $original_transparency);
  14.     $original_transparency = ($rgb['red'] << 16) | ($rgb['green'] << 8) | $rgb['blue'];
  15.     //change the transparent color to black, since transparent goes to black anyways (no way to remove transparency in GIF)
  16.     imagecolortransparent($img, imagecolorallocate($img, 0, 0, 0));
  17.   }
  18.   //create truecolor image and transfer
  19.   $truecolor = imagecreatetruecolor($w, $h);
  20.   imagealphablending($img, false);
  21.   imagesavealpha($img, true);
  22.   imagecopy($truecolor, $img, 0, 0, 0, 0, $w, $h);
  23.   imagedestroy($img);
  24.   $img = $truecolor;
  25.   //remake transparency (if there was transparency)
  26.   if ($original_transparency >= 0) {
  27.     imagealphablending($img, false);
  28.     imagesavealpha($img, true);
  29.     for ($x = 0; $x < $w; $x++)
  30.       for ($y = 0; $y < $h; $y++)
  31.         if (imagecolorat($img, $x, $y) == $original_transparency)
  32.           imagesetpixel($img, $x, $y, 127 << 24);
  33.   }
  34. }
  35. imagejpeg($img);
  36. ?>
Jul 21 '07 #1
Share this Question
Share on Google+
3 Replies


pbmods
Expert 5K+
P: 5,821
Heya, Rajaaa.

Please use CODE tags when posting source code. See the REPLY GUIDELINES on the right side of the page next time you post.

What do you want your code to do? Give an example.
What is your code doing that you don't want it to do? Give an example.
What is your code *not* doing that it is supposed to? Give an example.
Jul 21 '07 #2

P: 22
Hey,
Try using fopen to open the image then write to it with fwrite.
Example:
[php]
<?php
$newfilename = 'newfile,jpg';
$myfile = file_get_contents('winter.jpg');
$newfile = fopen($newfilename , 'w');
fwrite($newfile , $myfile);
?>
[/php]

Unless you are transforming the image to something differant which to me it looks like that was not the desired effect.
Jul 22 '07 #3

kovik
Expert 100+
P: 1,044
Hey,
Try using fopen to open the image then write to it with fwrite.
Example:
[php]
<?php
$newfilename = 'newfile,jpg';
$myfile = file_get_contents('winter.jpg');
$newfile = fopen($newfilename , 'w');
fwrite($newfile , $myfile);
?>
[/php]

Unless you are transforming the image to something differant which to me it looks like that was not the desired effect.
Why else would they be using the GD library? Did you look at the code? It's doing much more than that.



There are two things wrong with the code.
1) The "Content-type" header should be capitalized
2) That code does absolutely nothing to images that don't have transparency (i.e. JPEG files)
Jul 22 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.