开发者

PHP Image manipulation moire effect

开发者 https://www.devze.com 2023-04-05 08:52 出处:网络
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 backgroun

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.html where 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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消