1

I have a script that I am using to scale original images on my server. Sometimes the script works perfect and sometimes it doesn't scale it properly. However, it looks like the black area of the target is correct it just didn't actually scale the image. Why is it so unpredictable?

// check
  if($original_width > $original_height){ // landscape
    // vars
    $scale_ratio = $requested_width/$original_width;
    $width = $requested_width;
    $height = $original_height * $scale_ratio;
  }
  else if($original_height > $original_width){ // portrait
    // vars
    $scale_ratio = $requested_height/$original_height;
    $width = $original_width * $scale_ratio;
    $height = $requested_height;
  }
  else{ // square
    // vars
    $scale_ratio = $requested_height/$original_height;
    $width = $original_width * $scale_ratio;
    $height = $requested_height;
  }

  // create
  $target = imagecreatetruecolor($width, $height);
  $source = imagecreatefromjpeg($filename);

  // copy
  imagecopyresampled($target, $source, 0, 0, 0, 0, $width, $height, $original_width, $original_height);

  // set
  imagejpeg($target, null, $requested_quality);

First load of image

Second load of image

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Carey Richardson
  • 191
  • 2
  • 11
  • 1
    Use a library. I can't recommend http://image.intervention.io/ enough. – ceejayoz Jul 30 '16 at 23:03
  • Try scaling the target and not the original (uploaded image). Best to use an existing lib like ceejayoz recommends. – Nitin Jul 30 '16 at 23:06
  • I'm not sure what you mean by scaling the target not the original. That is what I am doing. I don't want to install all that stuff on my server just to get an image to scale. I prefer to use my own code and not a bunch of bloated third party code. I feel it keeps the bug and tracking errors to a minimum, especially when this is for an iPhone app. Php should be able to scale consistently. I tell the code to scale the image in question to a max size. It should really be about the easiest thing to do. – Carey Richardson Jul 31 '16 at 21:10
  • This answer might help... http://stackoverflow.com/questions/18805497/php-resize-image-on-upload/40324941#40324941 – HoldOffHunger Oct 30 '16 at 15:00

0 Answers0