Advertisement
Guest User

Untitled

a guest
Aug 24th, 2019
742
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1. /**
  2. *
  3. *
  4. * @param int $width
  5. * @param int $height
  6. * @param string $default
  7. */
  8. public function resize($width = 0, $height = 0, $default = '') {
  9. if (!$this->width || !$this->height) {
  10. return;
  11. }
  12.  
  13. $width = ($width > 0) ? $width : $this->width;
  14. $height = ($height > 0) ? $height : $this->height;
  15.  
  16. $xpos = 0;
  17. $ypos = 0;
  18. $scale = 1;
  19.  
  20. $scale_w = $width / $this->width;
  21. $scale_h = $height / $this->height;
  22.  
  23. if ($default == 'w') {
  24. $scale = $scale_w;
  25. } elseif ($default == 'h') {
  26. $scale = $scale_h;
  27. } else {
  28. $scale = min($scale_w, $scale_h);
  29. }
  30.  
  31. if ($scale == 1 && $scale_h == $scale_w && $this->mime != 'image/png') {
  32. return;
  33. }
  34.  
  35. $new_width = (int)($this->width * $scale);
  36. $new_height = (int)($this->height * $scale);
  37.  
  38. $image_old = $this->image;
  39. $this->image = imagecreatetruecolor($new_width, $new_height);
  40.  
  41. if ($this->mime == 'image/png') {
  42. imagealphablending($this->image, false);
  43. imagesavealpha($this->image, true);
  44. $background = imagecolorallocatealpha($this->image, 255, 255, 255, 127);
  45. imagecolortransparent($this->image, $background);
  46. } else {
  47. $background = imagecolorallocate($this->image, 255, 255, 255);
  48. }
  49.  
  50. imagefilledrectangle($this->image, 0, 0, $new_width, $new_height, $background);
  51.  
  52. imagecopyresampled($this->image, $image_old, $xpos, $ypos, 0, 0, $new_width, $new_height, $this->width, $this->height);
  53. imagedestroy($image_old);
  54.  
  55. $this->width = $new_width;
  56. $this->height = $new_height;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement