I am making a site where people can upload their own background images. One of the things users really having a problem understanding is image sizes and resolution. If I say 'Make sure your background is at least 1080x1920 pixels (Full HiDef)' I am guessing a good 70% of people will not know what this means or how to do it.

So what I would like to do is enlarge small images in a nicer way than just making them blurry. What I would like to do is something like this:-http://nanotux.com/plugins/fullscreenr/index.htmlwhere basically every other dot is a black pixel, that way small images will be twice the size when they are stretched to 1080x1920 and generally look better.

Does anyone know of a way to do this with PHP's image functions?(as an aside does anyone know what this type of effect should be called? Moire? would that be accurate?)

Thanks in advance

2

Best Answer


If I understood you well, you want to set every second pixel in x and y dimensions to black on the stretched image?

This should do the trick (not tested, I relied on PHP documentation).

 $initialImage = ... // handle to the image$srcWidth = imagesx($initialImage);$srcHeight = imagesy($initialImage);$maxX = 1920;$maxY = 1080;$newImage = imagecreatetruecolor($max_x, $max_y);imagecopyresampled ($newImage, $initialImage, 0,0,0,0, $maxX, $maxY, $srcWidth, $srcHeight);$BLACK = imagecolorallocate($newImage, 0, 0, 0);for($x=0; $x+=2; $x<$max_x){for($y=0; $y+=2; $y<$max_y){imagesetpixel($newImage, $x, $y, $BLACK);}}

Documentation: imagesetpixel, imagecolorallocate, imagecopyresampled,imagecreatetruecolor.

Read PHP documentation and examples there. Remember to use imagecopyresampled instead of imagecopyresized to have better quality.

The most optimal way would be to create a transparent raster (.png) "by hand" (i.e. create it programmatically as in jakub.gieryluk's solution), and overlay that, multiple times if needed, via imagecopy().

Drawing pixel by pixel is painfully slow.