Copy Source | Copy HTML<br/> // "" <br/> // Philips Ambilight <br/> // : <br/> // $image_from - <br/> // $image_to - <br/> // $width - "" <br/> // $height - "" <br/> // $padding - ( = 5) <br/> // $dest_image_type - ( JPG): <br/> // 2 - JPG <br/> // 3 - PNG <br/> // : <br/> // TRUE - <br/> // FALSE - <br/> function aidsoid_resize_image( $image_from , $image_to , $width = 200 , $height = 200 , $padding = 5 , $dest_image_type = 2 ) {<br/> $image_vars = getimagesize( $image_from );<br/> if (! $image_vars ) {<br/> return FALSE;<br/> }<br/> $src_width = $image_vars [ 0 ];<br/> $src_height = $image_vars [ 1 ];<br/> $src_type = $image_vars [ 2 ];<br/> if (( $src_type != IMAGETYPE_JPEG) and ( $src_type != IMAGETYPE_PNG)) {<br/> return FALSE;<br/> }<br/> <br/> switch ( $src_type ) {<br/> case IMAGETYPE_JPEG:<br/> $src_image = imagecreatefromjpeg( $image_from );<br/> break ;<br/> case IMAGETYPE_PNG:<br/> $src_image = imagecreatefrompng( $image_from );<br/> break ;<br/> default :<br/> return FALSE;<br/> break ;<br/> }<br/> if (! $src_image ) return FALSE;<br/> <br/> // <br/> $dest_image = imagecreatetruecolor( $width , $height );<br/> <br/> // <br/> $result = imagecopyresized( $dest_image , $src_image , 0 , 0 , 0 , 0 , $width , $height , $src_width , $src_height );<br/> if (! $result ) return FALSE;<br/> <br/> // <br/> for ( $i = 0 ; $i <= 100 ; $i ++) {<br/> $result = imagefilter( $dest_image , IMG_FILTER_SMOOTH, 6 );<br/> if (! $result ) return FALSE;<br/> }<br/> <br/> // - <br/> if ( ( $src_width == 0 ) or ( $src_height == 0 ) ) {<br/> return FALSE;<br/> }<br/> <br/> // <br/> $ratio = min( ( $width - 2 * $padding )/ $src_width , ( $height - 2 * $padding )/ $src_height );<br/> <br/> // <br/> $new_width = $ratio * $src_width ;<br/> $new_height = $ratio * $src_height ;<br/> <br/> // , <br/> // , , , <br/> // <br/> if ( ( $new_width >= $src_width ) or ( $new_height >= $src_height ) ) {<br/> $new_width = $src_width ;<br/> $new_height = $src_height ;<br/> }<br/> <br/> // <br/> $result = imagecopyresampled( $dest_image , $src_image , round(( $width - $new_width )/ 2 ), round(( $height - $new_height )/ 2 ), 0 , 0 , $new_width , $new_height , $src_width , $src_height );<br/> if (! $result ) return FALSE;<br/> <br/> // <br/> switch ( $dest_image_type ) {<br/> case IMAGETYPE_JPEG: // 100 <br/> $result = imagejpeg( $dest_image , $image_to , 100 );<br/> break ;<br/> case IMAGETYPE_PNG: // 9 <br/> $result = imagepng( $dest_image , $image_to , 9 );<br/> break ;<br/> default :<br/> return FALSE;<br/> break ;<br/> }<br/> if (! $result ) return FALSE;<br/> chmod( $image_to , 0777 );<br/> <br/> return TRUE;<br/>} <br/>
Copy Source | Copy HTML<br/>aidsoid_resize_image( "image.jpg" , "i/news/image-mini.jpg" , 200 , 200 , 5 , 2 ); <br/>
Copy Source | Copy HTML<br/> function pixelate(& $image ) {<br/> $imagex = imagesx( $image );<br/> $imagey = imagesy( $image );<br/> $blocksize = 5 ;<br/> <br/> for ( $x = 0 ; $x < $imagex ; $x += $blocksize ) {<br/> for ( $y = 0 ; $y < $imagey ; $y += $blocksize ) {<br/> // get the pixel colour at the top-left of the square <br/> $thiscol = imagecolorat( $image , $x , $y );<br/> <br/> // set the new red, green, and blue values to 0 <br/> $newr = 0 ;<br/> $newg = 0 ;<br/> $newb = 0 ;<br/> <br/> // create an empty array for the colours <br/> $colours = array ();<br/> <br/> // cycle through each pixel in the block <br/> for ( $k = $x ; $k < $x + $blocksize ; ++ $k ) {<br/> for ( $l = $y ; $l < $y + $blocksize ; ++ $l ) {<br/> // if we are outside the valid bounds of the image, use a safe colour <br/> if ( $k < 0 ) { $colours [] = $thiscol ; continue ; }<br/> if ( $k >= $imagex ) { $colours [] = $thiscol ; continue ; }<br/> if ( $l < 0 ) { $colours [] = $thiscol ; continue ; }<br/> if ( $l >= $imagey ) { $colours [] = $thiscol ; continue ; }<br/> <br/> // if not outside the image bounds, get the colour at this pixel <br/> $colours [] = imagecolorat( $image , $k , $l );<br/> }<br/> }<br/> <br/> // cycle through all the colours we can use for sampling <br/> foreach ( $colours as $colour ) {<br/> // add their red, green, and blue values to our master numbers <br/> $newr += ( $colour >> 16 ) & 0xFF ;<br/> $newg += ( $colour >> 8 ) & 0xFF ;<br/> $newb += $colour & 0xFF ;<br/> }<br/> <br/> // now divide the master numbers by the number of valid samples to get an average <br/> $numelements = count( $colours );<br/> $newr /= $numelements ;<br/> $newg /= $numelements ;<br/> $newb /= $numelements ;<br/> <br/> // and use the new numbers as our colour <br/> $newcol = imagecolorallocate( $image , $newr , $newg , $newb );<br/> imagefilledrectangle( $image , $x , $y , $x + $blocksize - 1 , $y + $blocksize - 1 , $newcol );<br/> }<br/> }<br/>}<br/> <br/> // "" <br/> // Philips Ambilight <br/> // : <br/> // $image_from - <br/> // $image_to - <br/> // $width - "" <br/> // $height - "" <br/> // $padding - ( = 5) <br/> // $dest_image_type - ( JPG): <br/> // 2 - JPG <br/> // 3 - PNG <br/> // : <br/> // TRUE - <br/> // FALSE - <br/> function aidsoid_resize_image( $image_from , $image_to , $width = 200 , $height = 200 , $padding = 5 , $dest_image_type = 2 ) {<br/> $image_vars = getimagesize( $image_from );<br/> if (! $image_vars ) {<br/> return FALSE;<br/> }<br/> $src_width = $image_vars [ 0 ];<br/> $src_height = $image_vars [ 1 ];<br/> $src_type = $image_vars [ 2 ];<br/> // - <br/> if ( ( $src_width == 0 ) or ( $src_height == 0 ) ) {<br/> return FALSE;<br/> }<br/> // <br/> if (( $src_type != IMAGETYPE_JPEG) and ( $src_type != IMAGETYPE_PNG)) {<br/> return FALSE;<br/> }<br/> <br/> switch ( $src_type ) {<br/> case IMAGETYPE_JPEG:<br/> $src_image = imagecreatefromjpeg( $image_from );<br/> break ;<br/> case IMAGETYPE_PNG:<br/> $src_image = imagecreatefrompng( $image_from );<br/> break ;<br/> default :<br/> return FALSE;<br/> break ;<br/> }<br/> if (! $src_image ) return FALSE;<br/> <br/> // <br/> $dest_image = imagecreatetruecolor( $width , $height );<br/> <br/> // <br/> $result = imagecopyresized( $dest_image , $src_image , 0 , 0 , 0 , 0 , $width , $height , $src_width , $src_height );<br/> if (! $result ) return FALSE;<br/> <br/> // <br/> $ratio = min( ( $width - 2 * $padding )/ $src_width , ( $height - 2 * $padding )/ $src_height );<br/> <br/> // <br/> $new_width = $ratio * $src_width ;<br/> $new_height = $ratio * $src_height ;<br/> <br/> // , <br/> // , , , <br/> // <br/> if ( ( $new_width >= $src_width ) or ( $new_height >= $src_height ) ) {<br/> $new_width = $src_width ;<br/> $new_height = $src_height ;<br/> }<br/> <br/> // <br/> $result = imagecopyresampled( $dest_image , $src_image , round(( $width - $new_width )/ 2 ), round(( $height - $new_height )/ 2 ), 0 , 0 , $new_width , $new_height , $src_width , $src_height );<br/> if (! $result ) return FALSE;<br/> <br/> // <br/> pixelate( $dest_image );<br/> <br/> $tmp_x = round(( $width - $new_width )/ 2 );<br/> $tmp_y = round(( $height - $new_height )/ 2 );<br/> // <br/> $result = imagecopyresized( $dest_image , $dest_image , $tmp_x , 0 , $tmp_x , $tmp_y , $new_width , $tmp_y , $new_width , 1 );<br/> if (! $result ) return FALSE;<br/> // <br/> $result = imagecopyresized( $dest_image , $dest_image , $tmp_x , $tmp_y + $new_height , $tmp_x , $tmp_y + $new_height - 1 , $new_width , $tmp_y , $new_width , 1 );<br/> if (! $result ) return FALSE;<br/> // <br/> $result = imagecopyresized( $dest_image , $dest_image , 0 , $tmp_y , $tmp_x , $tmp_y , $tmp_x , $new_height , 1 , $new_height );<br/> if (! $result ) return FALSE;<br/> // <br/> $result = imagecopyresized( $dest_image , $dest_image , $tmp_x + $new_width , $tmp_y , $tmp_x + $new_width - 1 , $tmp_y , $tmp_x , $new_height , 1 , $new_height );<br/> if (! $result ) return FALSE;<br/> <br/> // <br/> for ( $i = 0 ; $i <= 50 ; $i ++) {<br/> $result = imagefilter( $dest_image , IMG_FILTER_SMOOTH, 6 );<br/> if (! $result ) return FALSE;<br/> }<br/> <br/> // <br/> $result = imagecopyresampled( $dest_image , $src_image , round(( $width - $new_width )/ 2 ), round(( $height - $new_height )/ 2 ), 0 , 0 , $new_width , $new_height , $src_width , $src_height );<br/> if (! $result ) return FALSE;<br/> <br/> // <br/> switch ( $dest_image_type ) {<br/> case IMAGETYPE_JPEG: // 100 <br/> $result = imagejpeg( $dest_image , $image_to , 100 );<br/> break ;<br/> case IMAGETYPE_PNG: // 9 <br/> $result = imagepng( $dest_image , $image_to , 9 );<br/> break ;<br/> default :<br/> return FALSE;<br/> break ;<br/> }<br/> if (! $result ) return FALSE;<br/> chmod( $image_to , 0777 );<br/> <br/> return TRUE;<br/>} <br/>
Source: https://habr.com/ru/post/92654/
All Articles