Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This function works perfectly for me for resizing jpg's and transparent (or not) gif's:</p> <pre><code>function resizeImage($originalImage, $toWidth, $toHeight, $isJPG) { // Get the original geometry and calculate scales list($width, $height) = getimagesize($originalImage); $xscale = $width / $toWidth; $yscale = $height / $toHeight; // Recalculate new size with default ratio if ($yscale &gt; $xscale) { $new_width = round($width * (1 / $yscale)); $new_height = round($height * (1 / $yscale)); } else { $new_width = round($width * (1 / $xscale)); $new_height = round($height * (1 / $xscale)); } // Resize the original image if ($isJPG) { $imageResized = imagecreatetruecolor($new_width, $new_height); $imageTmp = imagecreatefromjpeg($originalImage); imagecopyresampled($imageResized, $imageTmp, 0, 0, 0, 0, $new_width, $new_height, $width, $height); } else { //$imageResized = imagecreatetruecolor($new_width, $new_height); //$imageTmp = imagecreatefromgif ($originalImage); //imagecopyresampled($imageResized, $imageTmp, 0, 0, 0, 0, $new_width, $new_height, $width, $height); # what follows is for resizing a gif, transparent or not # http://ru2.php.net/imagecopyresampled # load/create images $imageTmp = imagecreatefromgif($originalImage); $imageResized = imagecreatetruecolor($new_width, $new_height); imagealphablending($imageResized, false); # get and reallocate transparency-color $transindex = imagecolortransparent($imageTmp); if ($transindex &gt;= 0) { $transcol = imagecolorsforindex($imageTmp, $transindex); $transindex = imagecolorallocatealpha( $imageResized, $transcol['red'], $transcol['green'], $transcol['blue'], 127 ); imagefill($imageResized, 0, 0, $transindex); } # resample imagecopyresampled($imageResized, $imageTmp, 0, 0, 0, 0, $new_width, $new_height, $width, $height); # restore transparency if ($transindex &gt;= 0) { imagecolortransparent($imageResized, $transindex); for ($y = 0; $y &lt; $new_height; ++$y) { for ($x = 0; $x &lt; $new_width; ++$x) { if (((imagecolorat($imageResized, $x, $y) &gt;&gt; 24) &amp; 0x7F) &gt;= 100) { imagesetpixel( $imageResized, $x, $y, $transindex ); } } } } # save GIF imagetruecolortopalette($imageResized, true, 255); imagesavealpha($imageResized, false); } return $imageResized; } </code></pre> <p>The original function is from PhpToys 1.0, and the part that works with transparent .gifs comes from <a href="http://www.php.net/manual/en/function.imagecopyresampled.php#89987" rel="nofollow noreferrer">this PHP docs comment</a>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload