SimpleImage::thumbnail PHP Method

thumbnail() public method

This function attempts to get the image to as close to the provided dimensions as possible, and then crops the remaining overflow (from the center) to get the image to be the size specified. Useful for generating thumbnails.
public thumbnail ( integer $width, integer | null $height = null ) : SimpleImage
$width integer
$height integer | null If omitted - assumed equal to $width
return SimpleImage
    function thumbnail($width, $height = null)
    {
        // Determine height
        $height = $height ?: $width;
        // Determine aspect ratios
        $current_aspect_ratio = $this->height / $this->width;
        $new_aspect_ratio = $height / $width;
        // Fit to height/width
        if ($new_aspect_ratio > $current_aspect_ratio) {
            $this->fit_to_height($height);
        } else {
            $this->fit_to_width($width);
        }
        $left = floor($this->width / 2 - $width / 2);
        $top = floor($this->height / 2 - $height / 2);
        // Return trimmed image
        return $this->crop($left, $top, $width + $left, $height + $top);
    }

Usage Example

Beispiel #1
0
         $imgx->load($src_img);
         #-- check orientation ------------
         if (!empty($exif['Orientation'])) {
             switch ($exif['Orientation']) {
                 case 3:
                     $imgx->rotate(180);
                     break;
                 case 6:
                     $imgx->rotate(90);
                     break;
                 case 8:
                     $imgx->rotate(-90);
                     break;
             }
         }
         $imgx->thumbnail(500, 300);
         $imgx->save($app['data_path'] . "/konfig/gmb_header_" . $id . ".jpg");
         $data['gmb_header'] = "gmb_header_" . $id . ".jpg";
     } catch (Exception $e) {
         $_SESSION['msg'] = "gambar header gagal di unggah/upload ....";
         $_SESSION['alt'] = "warning";
         header("location: " . $urlx->get_referer());
         exit;
     }
 }
 $appx->mq_encode('p_headline,p_slogan');
 $sql = "update " . $app['table']["konfig_bahasa"] . "\n\t\t\t\tset headline = '{$p_headline}', \n\t\t\t\t    id_bahasa = '{$p_bahasa}',\n\t\t\t\t    slogan = '{$p_slogan}',\n\t\t\t\t\tmeta_description = '{$p_metades}',\n\t\t\t\t\tmeta_keyword = '{$p_metakey}',\n\t\t\t\t\tgmb_header  = '{$data['gmb_header']}',\n\t\t\t\t\ttgl_modif = now()\n\t\t\t\twhere id = '{$p_id}'";
 // echo $sql;exit;
 $dbu->qry($sql);
 $_SESSION['msg'] = "Data Provinsi {$p_nama} Berhasil Di Update ....";
 $_SESSION['alt'] = "success";
All Usage Examples Of SimpleImage::thumbnail