title: How to fix image quality on PHP (GD)?
body: <p>I have been trying to solve the border-image problem for many days, but I still can't come to a decision.. I really need your help!</p>
  
  <p>I have code:</p>
  
  <pre><code>$image = imagecreatefromjpeg('https://vignette4.wikia.nocookie.net/matrix/images/1/1f/Monica_Bellucci_Dolce_and_Gabbana.jpg/revision/latest?cb=20130227074822');
  $w = imagesx($image);
  $h = imagesy($image);
  
  $border = imagecreatefrompng('http://meson.ad-l.ink/8mRbHMnS9/thumb.png');
  $borderW = imagesx($border);
  $borderH = imagesy($border);
  
  $borderSize = 70;
  
  
  // New image width and height
  
  $isHorizontalAlign = true;
  
  $coProp = $w / $h;
  
  if ($coProp &gt; 1)
      $isHorizontalAlign = false;
  
  if ($isHorizontalAlign) {
      $newWidth = $w - $borderSize * 2;
      $newHeight = $newWidth * ($h / $w);
  } else {
      $newHeight = $h - $borderSize * 2;
      $newWidth = $newHeight * ($w / $h);
  }
  
  
  // Transparent border
  
  $indent = imagecreatetruecolor($w, $h);
  
  //imagesavealpha($indent, true);
  $color = imagecolorallocatealpha($indent, 0, 0, 0, 127);
  imagefill($indent, 0, 0, $color);
  
  
  $paddingLeft = ($w - $newWidth) / 2;
  $paddingTop = ($h - $newHeight) / 2;
  
  imagecopyresampled($indent, $image, $paddingLeft, $paddingTop, 0, 0, $newWidth, $newHeight, $w, $h);
  
  
  // New border width
  
  $x1 = $newWidth;
  $x2 = $borderSize;
  $x3 = (int)($x1 / $x2);
  
  if ($x3 == $x1 / $x2)
      $bw = $borderSize;
  else {
      $x4 = $x1 - $x3 * $x2;
      $x5 = $x4 / $x3;
      $x2 = $x2 + $x5;
  
      $bw = $x2;
  }
  
  
  // New border height
  
  $y1 = $newHeight;
  $y2 = $borderSize;
  $y3 = (int)($y1 / $y2);
  
  if ($y3 == $y1 / $y2)
      $bh = $borderSize;
  else {
      $y4 = $y1 - $y3 * $y2;
      $y5 = $y4 / $y3;
      $y2 = $y2 + $y5;
  
      $bh = $y2;
  }
  
  
  // Horizontal
  
  $percent1 = $bw / $borderSize;
  
  $borderNewWidth1 = (int)$borderW * $percent1;
  $borderNewHeight1 = (int)$borderH * $percent1;
  
  $thumb1 = imagecreatetruecolor($borderNewWidth1, $borderNewHeight1);
  imagesavealpha($thumb1, true);
  $color = imagecolorallocatealpha($thumb1, 0, 0, 0, 127);
  imagefill($thumb1, 0, 0, $color);
  imagecopyresized($thumb1, $border, 0, 0, 0, 0, $borderNewWidth1, $borderNewHeight1, $borderW, $borderH);
  
  
  // Vertical
  
  $percent2 = $bh / $borderSize;
  
  $borderNewWidth2 = (int)$borderW * $percent2;
  $borderNewHeight2 = (int)$borderH * $percent2;
  
  $thumb2 = imagecreatetruecolor($borderNewWidth2, $borderNewHeight2);
  imagesavealpha($thumb2, true);
  $color = imagecolorallocatealpha($thumb2, 0, 0, 0, 127);
  imagefill($thumb2, 0, 0, $color);
  imagecopyresized($thumb2, $border, 0, 0, 0, 0, $borderNewWidth2, $borderNewHeight2, $borderW, $borderH);
  
  
  // Angles
  
  $borderNewWidth3 = (int)$borderW * $percent1;
  $borderNewHeight3 = (int)$borderH * $percent2;
  
  $thumb3 = imagecreatetruecolor($borderNewWidth3, $borderNewHeight3);
  imagesavealpha($thumb3, true);
  $color = imagecolorallocatealpha($thumb3, 0, 0, 0, 127);
  imagefill($thumb3, 0, 0, $color);
  imagecopyresized($thumb3, $border, 0, 0, 0, 0, $borderNewWidth3, $borderNewHeight3, $borderW, $borderH);
  
  
  // Horizontal border
  
  $horizontalX = ($w - $newWidth) / 2;
  $horizontalY = (($h - $newHeight) / 2 - $bw) + 1;
  $horizontalY2 = $h - ($h - $newHeight) / 2;
  
  for ($i = 0; $i &lt; round($newWidth / $bw); $i++) {
      // Top
      imagecopy($indent, $thumb1, $horizontalX + ($i * $bw), $horizontalY, $borderSize * $percent1, 0, $bw + 1, $bw);
  
      // Bottom
      imagecopy($indent, $thumb1, $horizontalX + ($i * $bw), $horizontalY2, $borderSize * $percent1, $borderSize * 2 * $percent1, $bw + 1, $bw - 1);
  }
  
  
  // Vertical border
  
  $verticalY = ($h - $newHeight) / 2;
  $verticalX = (($w - $newWidth) / 2 - $bh) + 1;
  $verticalX2 = $w - ($w - $newWidth) / 2;
  
  for ($i = 0; $i &lt; round($newHeight / $bh); $i++) {
      // Left
      imagecopy($indent, $thumb2, $verticalX, $verticalY + ($i * $bh), 0, $borderSize * $percent2, $bh, $bh + 1);
  
      // Right
      imagecopy($indent, $thumb2, $verticalX2, $verticalY + ($i * $bh), ($borderSize * $percent2) * 2, $borderSize * $percent2, $bh, $bh + 1);
  }
  
  
  // Left top border
  imagecopy($indent, $thumb3, (($w - $newWidth) / 2 - $bw) + 1, (($h - $newHeight) / 2 - $bh) + 1, 0, 0, $bw, $bh);
  
  // Left bottom border
  imagecopy($indent, $thumb3, (($w - $newWidth) / 2 - $bw) + 1, $h - ($h - $newHeight) / 2, 0, ($borderSize * 2) * $percent2, $bw, $bh);
  
  // Right top border
  imagecopy($indent, $thumb3, $w - ($w - $newWidth) / 2, (($h - $newHeight) / 2 - $bh) + 1, ($borderSize * 2) * $percent1, 0, $bw, $bh);
  
  // Right bottom border
  imagecopy($indent, $thumb3, $w - ($w - $newWidth) / 2, $h - ($h - $newHeight) / 2, ($borderSize * 2) * $percent1, ($borderSize * 2) * $percent2, $bw, $bh);
  
  // Save result to base64
  
  header('Content-Type: image/png');
  imagepng($indent);
  </code></pre>
  
  <p>(by default I have a transparent background, but for an example I removed it)</p>
  
  <p>When image sizes are large - everything works perfectly:
  <a href="https://i.gyazo.com/028f5834efe968a7295194600fac684b.png" rel="nofollow noreferrer">https://i.gyazo.com/028f5834efe968a7295194600fac684b.png</a></p>
  
  <p>But when I add a small image, it's quality is significantly reduced. Look at this:
  <a href="https://i.stack.imgur.com/bkMql.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/bkMql.png" alt="enter image description here"></a></p>
  
  <p>I do not know how to fix this..</p>
  
  <p>P.S.: I'm trying to do frame for my photo using similar border-image: <a href="http://polariton.ad-l.ink/8mRbHMnS9/thumb.png" rel="nofollow noreferrer">http://polariton.ad-l.ink/8mRbHMnS9/thumb.png</a>
  The calculations that I did cost a lot of effort, but the result I could not achieve..</p>
  
  <p>Thank you for attention!
  I hope to solve the problem.</p>