note 126972 added to function.imagecropauto

From: Date: Wed, 06 Apr 2022 18:21:40 +0000
Subject: note 126972 added to function.imagecropauto
Groups: php.notes 
Request: Send a blank email to php-notes+get-218132@lists.php.net to get a copy of this message
I don’t know why you can’t set the threshold for the four sides filter (IMG_CROP_SIDES) so here’s how to do it manually using the IMG_CROP_THRESHOLD filter instead. $threshold = .5; $im = imagecreatefromjpeg('somefile.jpg'); $width = imagesx($im); $height = imagesy($im); $arr = [ [0,0], [$width-1,0], [0,$height-1], [$width-1,$height-1], ]; $red = 0; $green = 0; $blue = 0; // grab the colours from all four corners foreach( $arr as $arr2 ) { $thisColor = imagecolorat($im, $arr2[0], $arr2[1]); $rgb = imagecolorsforindex($im, $thisColor); $red += round(round(($rgb['red'] / 0x33)) * 0x33); $green += round(round(($rgb['green'] / 0x33)) * 0x33); $blue += round(round(($rgb['blue'] / 0x33)) * 0x33); } // and average them $red /= 4; $green /= 4; $blue /= 4; $newColor = imagecolorallocate($im, $red, $green, $blue); $cropped = imagecropauto($im, IMG_CROP_THRESHOLD, $threshold, $newColor); imagejpg($cropped, 'somefile.cropped.jpg'); imagedestroy($im); imagedestroy($cropped); ---- Server IP: 45.112.84.4 Probable Submitter: 80.90.5.135 (proxied: 99.251.81.58) ---- Manual Page -- https://php.net/manual/en/function.imagecropauto.php Edit -- https://main.php.net/note/edit/126972 Del: integrated -- https://main.php.net/note/delete/126972/integrated Del: useless -- https://main.php.net/note/delete/126972/useless Del: bad code -- https://main.php.net/note/delete/126972/bad+code Del: spam -- https://main.php.net/note/delete/126972/spam Del: non-english -- https://main.php.net/note/delete/126972/non-english Del: in docs -- https://main.php.net/note/delete/126972/in+docs Del: other reasons-- https://main.php.net/note/delete/126972 Reject -- https://main.php.net/note/reject/126972 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#218132) next »