Copy Image

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Rajaaa
    New Member
    • Jul 2007
    • 13

    Copy Image

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


    [code=php]
    <?php
    header("content-type:image/jpg");
    $img=imagecreat efromjpeg("Wint er.jpg");
    //Convert $img to truecolor
    echo $w = imagesx($img);
    echo $h = imagesy($img);

    if (!imageistrueco lor($img)) {
    $original_trans parency = imagecolortrans parent($img);
    //we have a transparent color
    if ($original_tran sparency >= 0) {
    //get the actual transparent color
    $rgb = imagecolorsfori ndex($img, $original_trans parency);
    $original_trans parency = ($rgb['red'] << 16) | ($rgb['green'] << 8) | $rgb['blue'];
    //change the transparent color to black, since transparent goes to black anyways (no way to remove transparency in GIF)
    imagecolortrans parent($img, imagecoloralloc ate($img, 0, 0, 0));
    }
    //create truecolor image and transfer
    $truecolor = imagecreatetrue color($w, $h);
    imagealphablend ing($img, false);
    imagesavealpha( $img, true);
    imagecopy($true color, $img, 0, 0, 0, 0, $w, $h);
    imagedestroy($i mg);
    $img = $truecolor;
    //remake transparency (if there was transparency)
    if ($original_tran sparency >= 0) {
    imagealphablend ing($img, false);
    imagesavealpha( $img, true);
    for ($x = 0; $x < $w; $x++)
    for ($y = 0; $y < $h; $y++)
    if (imagecolorat($ img, $x, $y) == $original_trans parency)
    imagesetpixel($ img, $x, $y, 127 << 24);
    }
    }
    imagejpeg($img) ;
    ?>[/code]
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    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.

    Comment

    • tscott
      New Member
      • Jul 2007
      • 22

      #3
      Hey,
      Try using fopen to open the image then write to it with fwrite.
      Example:
      [php]
      <?php
      $newfilename = 'newfile,jpg';
      $myfile = file_get_conten ts('winter.jpg' );
      $newfile = fopen($newfilen ame , '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.

      Comment

      • kovik
        Recognized Expert Top Contributor
        • Jun 2007
        • 1044

        #4
        Originally posted by tscott
        Hey,
        Try using fopen to open the image then write to it with fwrite.
        Example:
        [php]
        <?php
        $newfilename = 'newfile,jpg';
        $myfile = file_get_conten ts('winter.jpg' );
        $newfile = fopen($newfilen ame , '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)

        Comment

        Working...