Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- kirby()->hook('panel.file.upload', function($file) {
- try {
- list($width, $height) = getimagesize($file->width());
- if ($width > $height) {
- // Landscape
- $file->resize(1200, 800);
- } else {
- // Portrait or Square
- createLandscape($file->url());
- }
- } catch (Exception $e) {
- throw new Exception($e->getMessage());
- }
- });
- function createLandscape($filePath){
- //$filePath = '003.jpg'; //full path to your png, including filename and extension
- $fileParts = pathinfo($filePath);
- $savePath = $filePath; //full path to saved png, including filename and extension
- $colorRgb = ['red' => 229, 'green' => 231, 'blue' => 232]; //background color
- if($fileParts['extension'] == 'jpg' || $fileParts['extension'] == 'jpeg'){
- $img = @imagecreatefromjpeg($filePath);
- $imgWidth = imagesx($img);
- $imgHeight = imagesy($img);
- $width = 1200;
- $height = 810;
- $endPosX = ($width - $imgWidth) / 2;
- }
- //create new image and fill with background color
- $backgroundImg = @imagecreatetruecolor($width, $height);
- $color = imagecolorallocate($backgroundImg, $colorRgb['red'], $colorRgb['green'], $colorRgb['blue']);
- imagefill($backgroundImg, 0, 0, $color);
- //copy original image to background
- imagecopy($backgroundImg, $img, $endPosX, 0, 0, 0, $imgWidth, $imgHeight);
- //save as jpg
- imagejpeg($backgroundImg, $savePath, 100);
- }
Advertisement
Add Comment
Please, Sign In to add comment