Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.08 KB | None | 0 0
  1. function perspective($i,$gradient=0.9,$rightdown=true,$background=0xFFFFFF) {
  2. $mult=3;
  3. $w=imagesx($i);
  4. $h=imagesy($i);
  5. $image=imagecreatetruecolor($w*$mult,$h*$mult);
  6. imagecopyresized($image,$i,0,0,0,0,$w*$mult,$h*$mult,$w,$h);
  7. imagedestroy($i);
  8. $w*=$mult;
  9. $h*=$mult;
  10. $im=imagecreatetruecolor($w,$h);
  11. $background=imagecolorallocate($im,($background>>16)&0xFF,($background>>8)&0xFF,$background&0xFF);
  12. imagefill($im,0,0,$background);
  13. imageantialias($im,true);
  14. $nh=$h-($h*$gradient);
  15. for ($x=0; $x<$w; $x++) {
  16. $ni=(($rightdown) ? $x : $w-$x);
  17. $p=intval($h-(($ni/$w)*$nh));
  18. if (($p%2)<>0)
  19. $p-=1;
  20. $nx=intval(($p-$h)/2);
  21. imagecopyresampled($im,$image,$x,0,$x,$nx,1,$p,1,$h-1);
  22. imageline($im,$x,$h-1,$x,$h+$nx,$background);
  23. imageline($im,$x,0,$x,-$nx-1,$background);
  24. }
  25. imagedestroy($image);
  26. imagefilter($im,IMG_FILTER_SMOOTH,10);
  27. $i=imagecreatetruecolor($w/$mult,$h/$mult);
  28. imageantialias($i,true);
  29. imagecopyresampled($i,$im,0,0,0,0,$w,$h,$w*$mult,$h*$mult);
  30. imagedestroy($im);
  31. return $i;
  32. }
  33.  
  34. function makeTrapeziumImage($image, $gradient, $rightToLeft = false, $background = 0xFFFFFF, $supersampleScale = 3) {
  35. $originalWidth = imagesx($image);
  36. $originalHeight = imagesy($image);
  37.  
  38. $supersampledWidth = $originalWidth * $supersampleScale;
  39. $supersampledHeight = $originalHeight * $supersampleScale;
  40.  
  41. $supersampledImage = imagecreatetruecolor($supersampledWidth, $supersampledHeight);
  42.  
  43. imagecopyresized($supersampledImage, $image,
  44. 0, 0, 0, 0,
  45. $supersampledWidth, $supersampledHeight, $originalWidth, $originalHeight);
  46.  
  47. $workingImage = imagecreatetruecolor($supersampledWidth, $supersampledHeight);
  48.  
  49. $backgroundColour = imagecolorallocate($workingImage, ($background >> 16) & 0xFF, ($background >> 8) & 0xFF, $background & 0xFF);
  50. imagefill($workingImage, 0, 0, $backgroundColour);
  51.  
  52. imageantialias($workingImage,true);
  53.  
  54. $endHeight = $supersampledHeight - ($supersampledHeight * $gradient);
  55.  
  56. for ($x = 0; $x < $supersampledWidth; $x++) {
  57. $cX = ($rightToLeft ? $supersampledWidth - $x : $x);
  58.  
  59. $dstHeight = $supersampledHeight - ((($cX + 1) / $supersampledWidth) * $endHeight);
  60.  
  61. $dstY = intval(($supersampledHeight - $dstHeight) / 2) - 1; // -1 required as zero-indexed
  62. $dstY = ($dstY < 0 ? 0 : $dstY); // Rounding can make $dstY = -1
  63.  
  64. $dstHeight = intval($dstHeight); // Round the height after calculating $dstY
  65.  
  66. imagecopyresampled($workingImage, $supersampledImage,
  67. $cX, $dstY, $cX, 0,
  68. 1, $dstHeight, 1, $supersampledHeight);
  69. }
  70.  
  71. imagedestroy($supersampledImage);
  72. imagefilter($workingImage, IMG_FILTER_SMOOTH, 10);
  73.  
  74. $resizedImage = imagecreatetruecolor($originalWidth, $originalHeight);
  75. imageantialias($resizedImage, true);
  76.  
  77. imagecopyresampled($resizedImage, $workingImage,
  78. 0, 0, 0, 0,
  79. $originalWidth, $originalHeight, $supersampledWidth, $supersampledHeight);
  80.  
  81. imagedestroy($workingImage);
  82.  
  83. return $resizedImage;
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement