1

I am trying to create a simple, single file thumbnail generator.

This file takes the image url and generates a small image.

It works for .JPG, .PNG, .GIF images, but does not work for .WEBP.

Request

How to make my code also generate the image .WEBP?

Start

thumb.php?img=./product/image.webp

File: thumb.php

<?php
  $imagem = $_GET['imagem'];

  $extensao = strrchr($imagem, '.');

  if($extensao==".gif" or $extensao==".GIF") {
    header("Content-type: image/gif");
  }

  if($extensao==".jpg" or $extensao==".JPG") {
    header("Content-type: image/jpeg");
  }

  if($extensao==".png" or $extensao==".PNG") {
    header("Content-type: image/png");
  }

  if($extensao==".webp" or $extensao==".WEBP") {
    header("Content-type: image/webp");
  }



  $largura = 110;
  $altura = 88;

  $imagem = $imagem;
  $max_x = $largura;
  $max_y = $altura;



  if($extensao==".gif" or $extensao==".GIF") {
    $im = imagecreatefromgif($imagem);
  }

  if($extensao==".jpg" or $extensao==".JPG"){
    $im = imagecreatefromjpeg($imagem);
  }

  if($extensao==".png" or $extensao==".PNG"){
    $im = imagecreatefrompng($imagem);
  }

  if($extensao==".webp" or $extensao==".WEBP"){
    $im = imagecreatefromwebp($imagem);
  }



  if ($max_x != 0 && $max_y != 0) {
    $x = imagesx($im);
    $y = imagesy($im);

    if ($x > $max_x) {
      $y = (int)floor($y * ($max_x / $x));
      $x = $max_x;
    }

    if ($y > $max_y) {
      $x = (int)floor($x * ($max_y / $y));
      $y = $max_y;
    }
  }



  $img_dest = ImageCreateTrueColor($x, $y);

  ImageCopyResampled($img_dest,$im,0,0,0,0,$x,$y,imagesx($im),imagesy($im));



  if($extensao==".jpg" or $extensao==".JPG") {
    imagejpeg($img_dest, $arquivo_miniatura);
  }

  if($extensao==".png" or $extensao==".PNG") {
    imagepng($img_dest, $arquivo_miniatura);
  }

  if($extensao==".gif" or $extensao==".GIF") {
    imagegif($img_dest, $arquivo_miniatura);
  }

  if($extensao==".webp" or $extensao==".WEBP") {
    imagewebp($img_dest, $arquivo_miniatura);
  }



  if($extensao==".jpg" or $extensao==".JPG") {
    imagejpeg($img_dest);
    echo $arquivo_miniatura;
  }

  if($extensao==".png" or $extensao==".PNG") {
    imagepng($img_dest);
    echo $arquivo_miniatura;
  }

  if($extensao==".gif" or $extensao==".GIF") {
    imagegif($img_dest);
    echo $arquivo_miniatura;
  }

  if($extensao==".webp" or $extensao==".WEBP") {
    imagewebp($img_dest);
    echo $arquivo_miniatura;
  }

?>


Thanks.

Tiago
  • 797
  • 1
  • 10
  • 23

0 Answers0