Uploaded image for project: 'In-Portal CMS'
  1. In-Portal CMS
  2. INP-565

Add ability to generate "Rounded" and "Grayscale" images

    XMLWordPrintable

    Details

    • Type: Feature Request
    • Status: Closed
    • Priority: Minor
    • Resolution: Won't Fix
    • Affects Version/s: 5.1.0
    • Fix Version/s: 5.2.1-RC1
    • Component/s: Other
    • Labels:
      None
    • Additional information:
      Hide

      Converting to Rounded

      function imageRoundedCopyResampled(&$dstimg, &$srcimg, $dstx, $dsty, $srcx, $srcy, $dstw, $dsth, $srcw, $srch, $radius)
      {
      	# Resize the Source Image 
      	$srcResized = imagecreatetruecolor($dstw, $dsth);
      	
      	imagecopyresampled($srcResized, $srcimg, 0, 0, $srcx, $srcy, $dstw, $dsth, $srcw, $srch);
      	
      	# Copy the Body without corners 
      	imagecopy($dstimg, $srcResized, $dstx + $radius, $dsty, $radius, 0, $dstw - ($radius * 2), $dsth);
      	imagecopy($dstimg, $srcResized, $dstx, $dsty + $radius, 0, $radius, $dstw, $dsth - ($radius * 2));
      	
      	# Create a list of iterations; array(array(X1, X2, CenterX, CenterY), ...) 
      	# Iterations in order are: Top-Left, Top-Right, Bottom-Left, Bottom-Right 
      	$iterations = array(
      		array(0, 0, $radius, $radius),
      		array($dstw - $radius, 0, $dstw - $radius, $radius),
      		array(0, $dsth - $radius, $radius, $dsth - $radius),
      		array($dstw - $radius, $dsth - $radius, $dstw - $radius, $dsth - $radius)
      	);
      	
      	# Loop through each corner 'iteration' 
      	foreach ( $iterations as $iteration ) {
      		list($x1, $y1, $cx, $cy) = $iteration;
      		
      		for ( $y = $y1; $y <= $y1 + $radius; $y++ ) {
      			for ( $x = $x1; $x <= $x1 + $radius; $x++ ) {
      				# If length (X,Y)->(CX,CY) is less then radius draw the point 
      				$length = sqrt(pow(($cx - $x), 2) + pow(($cy - $y), 2));
      				
      				if ( $length < $radius ) {
      					imagecopy($dstimg, $srcResized, $x + $dstx, $y + $dsty, $x, $y, 1, 1);
      				}
      			}
      		}
      	}
      }
      

      could be a better solution, which does this:

      • draws white square based on source image dimensions
      • cuts circle from the midle
      • places original image under prepared image so only image part, that fits the hole is visible

      This approach will use standard GD function, like "imagefilledellipse" and
      so on.

      Converting to Grayscale

      $source_file = "test_image.jpg";
      
      $im = ImageCreateFromJpeg($source_file);
      
      $imgw = imagesx($im);
      $imgh = imagesy($im);
      
      for ( $i = 0; $i < $imgw; $i++ ) {
      	for ( $j = 0; $j < $imgh; $j++ ) {
      		// get the rgb value for current pixel 
      		$rgb = ImageColorAt($im, $i, $j);
      
      		// extract each value for r, g, b 
      		$rr = ($rgb >> 16) & 0xFF;
      		$gg = ($rgb >> 8) & 0xFF;
      		$bb = $rgb & 0xFF;
      
      		// get the Value from the RGB value 
      		$g = round(($rr + $gg + $bb) / 3);
      
      		// grayscale values have r=g=b=g 
      		$val = imagecolorallocate($im, $g, $g, $g);
      
      		// set the gray value 
      		imagesetpixel($im, $i, $j, $val);
      	}
      }
      
      header('Content-type: image/jpeg');
      imagejpeg($im); 
      
      Show
      Converting to Rounded function imageRoundedCopyResampled(& $dstimg , & $srcimg , $dstx , $dsty , $srcx , $srcy , $dstw , $dsth , $srcw , $srch , $radius ) { # Resize the Source Image $srcResized = imagecreatetruecolor( $dstw , $dsth ); imagecopyresampled( $srcResized , $srcimg , 0, 0, $srcx , $srcy , $dstw , $dsth , $srcw , $srch ); # Copy the Body without corners imagecopy( $dstimg , $srcResized , $dstx + $radius , $dsty , $radius , 0, $dstw - ( $radius * 2), $dsth ); imagecopy( $dstimg , $srcResized , $dstx , $dsty + $radius , 0, $radius , $dstw , $dsth - ( $radius * 2)); # Create a list of iterations; array ( array (X1, X2, CenterX, CenterY), ...) # Iterations in order are: Top-Left, Top-Right, Bottom-Left, Bottom-Right $iterations = array ( array (0, 0, $radius , $radius ), array ( $dstw - $radius , 0, $dstw - $radius , $radius ), array (0, $dsth - $radius , $radius , $dsth - $radius ), array ( $dstw - $radius , $dsth - $radius , $dstw - $radius , $dsth - $radius ) ); # Loop through each corner 'iteration' foreach ( $iterations as $iteration ) { list ( $x1 , $y1 , $cx , $cy ) = $iteration ; for ( $y = $y1 ; $y <= $y1 + $radius ; $y ++ ) { for ( $x = $x1 ; $x <= $x1 + $radius ; $x ++ ) { # If length (X,Y)->(CX,CY) is less then radius draw the point $length = sqrt(pow(( $cx - $x ), 2) + pow(( $cy - $y ), 2)); if ( $length < $radius ) { imagecopy( $dstimg , $srcResized , $x + $dstx , $y + $dsty , $x , $y , 1, 1); } } } } } could be a better solution, which does this: draws white square based on source image dimensions cuts circle from the midle places original image under prepared image so only image part, that fits the hole is visible This approach will use standard GD function, like "imagefilledellipse" and so on. Converting to Grayscale $source_file = "test_image.jpg"; $im = ImageCreateFromJpeg($source_file); $imgw = imagesx( $im ); $imgh = imagesy( $im ); for ( $i = 0; $i < $imgw ; $i ++ ) { for ( $j = 0; $j < $imgh ; $j ++ ) { // get the rgb value for current pixel $rgb = ImageColorAt( $im , $i , $j ); // extract each value for r, g, b $rr = ( $rgb >> 16) & 0xFF; $gg = ( $rgb >> 8) & 0xFF; $bb = $rgb & 0xFF; // get the Value from the RGB value $g = round(( $rr + $gg + $bb ) / 3); // grayscale values have r=g=b=g $val = imagecolorallocate( $im , $g , $g , $g ); // set the gray value imagesetpixel( $im , $i , $j , $val ); } } header( 'Content-type: image/jpeg' ); imagejpeg( $im );
    • Change Log Message:
      Ability to generate "Rounded" and "Grayscale" images on the fly
    • External issue ID:
      714
    • Copy Issue Key:
    • Patch Instructions:

      Patches must be submitted through Phabricator.

      Description

      Improve our current Image helper (already powerful tool) and teach new things like create new version of the image:

      • Rounded
      • Grayscale

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                alex Alex
                Reporter:
                dmitry Dmitry Andrejev [Intechnic]
                Developer:
                Dmitry Andrejev [Intechnic]
                Reviewer:
                Alex
              • Votes:
                0 Vote for this issue
                Watchers:
                1 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: