#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2011
    Posts
    13
    Rep Power
    0

    Add black color for on an image resized.


    I have a code that make a re-size and crop to a picture.
    First he resize the picture and after that put the picture in the middle of the white background.

    The only problem is if a picture is vertical add black color in the right side instead of white.



    For horizontal pictures it's ok.


    Why is adding the black color in the right side ?

    Thank you
  2. #2
  3. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6351
    There's a problem with line 38.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2011
    Posts
    13
    Rep Power
    0
    Sorry, I forgot to add the code:


    function resize_image($max_width,$max_height){

    $width = $this->getWidth($this->image_location);
    $height = $this->getHeight($this->image_location);

    $new_width = "";
    $new_height = "";

    $with_scale = $width/$max_width;
    $height_scale = $height/$max_height;

    if($with_scale > $height_scale){
    $new_width = $max_width;
    $new_height = ($max_width/$width) * $height;

    }else{
    $new_height = $max_height;
    $new_width = ($max_height/$height) * $width;

    }

    $x_mid = $new_width / 2;
    $y_mid = $new_height / 2;

    $newImage = imagecreatetruecolor($new_width,$new_height);
    $source = imagecreatefromjpeg($this->image_location);
    imagecopyresampled($newImage,$source,0,0,0,0,$new_width,$new_height,$width,$height);


    $final = imagecreatetruecolor($max_width, $max_height);
    imagecopyresampled($final, $newImage, 0, 0, ($x_mid - ($max_width / 2)), ($y_mid - ($max_height / 2)), $max_width, $max_height, $max_width, $max_height);
    $bg_color = imagecolorallocate ($final, 255, 255, 255);
    imagefill($final, 0, 0, $bg_color);


    imagejpeg($final,$this->new_location,80);
    chmod($this->new_location, 0777);
    return $this->new_location;
    }
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2011
    Posts
    13
    Rep Power
    0
    Anyone?
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    194
    Rep Power
    77
    im guessing you copied this from here?

    http://www.devnetwork.net/viewtopic.php?f=1&t=135893
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2011
    Posts
    13
    Rep Power
    0
    Originally Posted by jack13580
    im guessing you copied this from here?

    http://www.devnetwork.net/viewtopic.php?f=1&t=135893
    It was an old post of mine when the code is doing well.
    The black color start to appear 4weeks ago. I try to believe that the administrator do an upgrade to the php version on the server because I don't made any changes to the code in the last year.

    Any ideas?
  12. #7
  13. Wiser? Not exactly.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    May 2001
    Location
    Bonita Springs, FL
    Posts
    5,953
    Rep Power
    4033
    Do your imagefill operation before you copy the resized image over. imagefill will stop once it hits a boundary where the color changes so once your image has been copied you've created a boundary for it to stop at. By doing the fill first, it will fill in the entire image with white, then copy the resized image ontop of that white background.
    Recycle your old CD's, don't just trash them



    If I helped you out, show some love with some reputation, or tip with Bitcoins to 1N645HfYf63UbcvxajLKiSKpYHAq2Zxud
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2011
    Posts
    13
    Rep Power
    0
    Originally Posted by kicken
    Do your imagefill operation before you copy the resized image over. imagefill will stop once it hits a boundary where the color changes so once your image has been copied you've created a boundary for it to stop at. By doing the fill first, it will fill in the entire image with white, then copy the resized image ontop of that white background.

    I did what you told me and now I have black color in both sides (right/left). I need white color.

    The new code is:

    function resize_image($max_width,$max_height){

    $width = $this->getWidth($this->image_location);
    $height = $this->getHeight($this->image_location);

    $new_width = "";
    $new_height = "";

    $with_scale = $width/$max_width;
    $height_scale = $height/$max_height;

    if($with_scale > $height_scale){
    $new_width = $max_width;
    $new_height = ($max_width/$width) * $height;

    }else{
    $new_height = $max_height;
    $new_width = ($max_height/$height) * $width;

    }

    $x_mid = $new_width / 2;
    $y_mid = $new_height / 2;

    $newImage = imagecreatetruecolor($new_width,$new_height);
    $source = imagecreatefromjpeg($this->image_location);
    imagecopyresampled($newImage,$source,0,0,0,0,$new_width,$new_height,$width,$height);


    $final = imagecreatetruecolor($max_width, $max_height);
    $bg_color = imagecolorallocate ($final, 255, 255, 255);
    imagefill($final, 0, 0, $bg_color);
    imagecopyresampled($final, $newImage, 0, 0, ($x_mid - ($max_width / 2)), ($y_mid - ($max_height / 2)), $max_width, $max_height, $max_width, $max_height);



    imagejpeg($final,$this->new_location,80);
    chmod($this->new_location, 0777);
    return $this->new_location;
    }
  16. #9
  17. Wiser? Not exactly.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    May 2001
    Location
    Bonita Springs, FL
    Posts
    5,953
    Rep Power
    4033
    For your second copy of the image, just use imagecopy, not imagecopyresampled. You are not resizing the image so there is no need for it to be resampled.

    Code:
    $dst_x = ($max_width - $new_width)/2;
    $dst_y = ($max_height - $new_height)/2;
    
    imagecopy($final, $newImage, $dst_x, $dst_y, 0, 0, $new_width, $new_height);
    Recycle your old CD's, don't just trash them



    If I helped you out, show some love with some reputation, or tip with Bitcoins to 1N645HfYf63UbcvxajLKiSKpYHAq2Zxud
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2011
    Posts
    13
    Rep Power
    0
    Originally Posted by kicken
    For your second copy of the image, just use imagecopy, not imagecopyresampled. You are not resizing the image so there is no need for it to be resampled.

    Code:
    $dst_x = ($max_width - $new_width)/2;
    $dst_y = ($max_height - $new_height)/2;
    
    imagecopy($final, $newImage, $dst_x, $dst_y, 0, 0, $new_width, $new_height);
    It's working. Thank you very much.

IMN logo majestic logo threadwatch logo seochat tools logo