1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | <?php // WORKS FOR JPEG IMAGES ONLY. FOR PNG, change function names // also create a folder named "out" in the $datadir folder ///only 4 lines to be edited //1. dir in which your image files are present $datadir = "J:/dgcert/" ; //2. Expected min. Width of the output file $_outw =840; //3. Expected min. Height of the output file $_outh =840; //4. output folder (inside datadir) $outfolder = '/out/' ; //5. force final size -- -- If set to 1/true then line 2 and 3 will become max sizes of output image // TO BE DONE IN FUTURE . does not work as of now $forcesize =0; ////// no more edits needed $files = array_diff (scandir( $datadir ), array ( "." , ".." )); foreach ( $files as $file ) { if ( is_file ( $datadir . '/' . $file )){ $outw = $_outw ; $outh = $_outh ; $mugl_im = imagecreatefromjpeg( $datadir . $file ); list( $thiswidth , $thisheight )= getimagesize ( $datadir . $file ); if ( $outw < $thiswidth ) $outw = $thiswidth ; if ( $outh < $thisheight ) $outh = $thisheight ; if ( $outw < $outh ) $outw = $outh ; if ( $outh < $outw ) $outh = $outw ; //echo $width . $height;exit; $thumb = imagecreatetruecolor( $outw , $outh ); //white color rgb $bg = imagecolorallocate ( $thumb , 255, 255, 255 ); imagefilledrectangle( $thumb ,0,0, $outw , $outh , $bg ); imagecopyresized( $thumb , $mugl_im , ceil (( $outw - $thiswidth )/2), ceil (( $outh - $thisheight )/2),0,0, $thiswidth , $thisheight , $thiswidth , $thisheight ); imagejpeg( $thumb , $datadir . $outfolder . $file ); imagedestroy( $thumb ); imagedestroy( $mugl_im ); } } ?> |
Leave a Reply