Giter VIP home page Giter VIP logo

jdenticon-php's Introduction

PHP library for generating highly recognizable identicons.

Sample identicons

Test Status Total Downloads

Features

Jdenticon-php is a PHP port of the JavaScript library Jdenticon.

  • Renders identicons as PNG or SVG with no extension requirements.
  • Runs on PHP 5.3 and later.

Live demo

https://jdenticon.com

Getting started

Using Jdenticon is simple. Follow the steps below to integrate Jdenticon into your website.

1. Install the Jdenticon Composer package

The easiest way to get started using Jdenticon for PHP is to install the Jdenticon Composer package.

composer require jdenticon/jdenticon

2. Create a php file that will serve an icon

Now create a file that you call icon.php and place it in the root of your application. Add the following content to the file.

<?php
include_once("vendor/autoload.php");

// Set max-age to a week to benefit from client caching (this is optional)
header('Cache-Control: max-age=604800');

// Parse query string parameters
$value = $_GET['value'];
$size = min(max(intval($_GET['size']), 20), 500);

// Render icon
$icon = new \Jdenticon\Identicon();
$icon->setValue($value);
$icon->setSize($size);
$icon->displayImage('png');

3. Use icon.php

Open up your favourite browser and navigate to http://localhost:PORT/icon.php?size=100&value=anything. An identicon should be displayed. Try to change the url parameters to see the difference in the generated icon.

Other resources

API documentation

For more usage examples and API documentation, please see:

https://jdenticon.com/php-api.html

License

Jdenticon-php is released under the MIT license.

jdenticon-php's People

Contributors

dmester avatar rvxlab avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

jdenticon-php's Issues

Issues with the native ImageMagick-based renderer

Jdenticon is bundled with an internal PNG renderer. There is no issue with the internal renderer, except it does not perform very well on systems running PHP 5.6 or earlier.

As an alternative ImageMagick was provided as a native renderer in version 0.9.0. However there are several issues with the ImageMagick based PNG renderer:

  • ImagickDraw cannot be used directly to rasterize since the API draws all polygons a pixel too large, leading to artifacts in the resulting icons (documented here and here).
  • When using ImageMagick for converting SVG to PNG the extra codec package (libmagickcore-xxx-extra) is required on Linux systems. Jdenticon should be a library that works out of the box.
  • If Inkscape is not installed the internal ImageMagick SVG renderer is used, which, like ImagickDraw, renders all polygons a pixel too large.
  • If Inkscape is installed the polygons are rendered with the correct size, however on some systems the resulting PNG has the wrong dimensions.

If no good solution for these issues are found the ImageMagick support will be removed from Jdenticon in the upcoming release.

Alternative libraries:

  • GD is unfortunately not an alternative to ImageMagick, since it does not support anti aliasing.
  • Cairo could be an alternative. However probably not as commonly installed as ImageMagick or GD.
  • Check if Gmagick has the same issues as ImageMagick.

Wow, I didn't see that coming.

Hi @dmester! I just stumbled on your new repository, after I spent the last two days porting jdenticon to PHP (well, actually only the SVG part). Most bugs seem to be fixed now, except that I can't exactly reproduce the test SVG.

Well, it was an interesting exercise, even if I ultimately get to switch to an official version. Thanks for your great work.

PHP 8.2: Dynamic Properties are deprecated

Hello and thank you for maintaining this project. When running this library under PHP 8.2 in my projects unit tests, deprecation warnings get emitted:

Deprecated: Creation of dynamic property Jdenticon\Canvas\Rasterization\SuperSampleBuffer::$samples is deprecated in /home/runner/work/PrivateBin/PrivateBin/vendor/jdenticon/jdenticon/src/Canvas/Rasterization/SuperSampleBuffer.php on line 33
Deprecated: Creation of dynamic property Jdenticon\Canvas\Rasterization\SuperSampleBuffer::$samplesPerPixel is deprecated in /home/runner/work/PrivateBin/PrivateBin/vendor/jdenticon/jdenticon/src/Canvas/Rasterization/SuperSampleBuffer.php on line 34
Deprecated: Creation of dynamic property Jdenticon\Canvas\Rasterization\SuperSampleBuffer::$pixelOffset is deprecated in /home/runner/work/PrivateBin/PrivateBin/vendor/jdenticon/jdenticon/src/Canvas/Rasterization/SuperSampleBuffer.php on line 36
Deprecated: Creation of dynamic property Jdenticon\Canvas\Rasterization\SuperSampleBuffer::$subPixelOffset is deprecated in /home/runner/work/PrivateBin/PrivateBin/vendor/jdenticon/jdenticon/src/Canvas/Rasterization/SuperSampleBuffer.php on line 37
Deprecated: Creation of dynamic property Jdenticon\Canvas\Rasterization\SuperSampleBuffer::$width is deprecated in /home/runner/work/PrivateBin/PrivateBin/vendor/jdenticon/jdenticon/src/Canvas/Rasterization/SuperSampleBuffer.php on line 39
Deprecated: Creation of dynamic property Jdenticon\Canvas\Rasterization\SuperSampleBuffer::$used is deprecated in /home/runner/work/PrivateBin/PrivateBin/vendor/jdenticon/jdenticon/src/Canvas/Rasterization/SuperSampleBuffer.php on line 40

The test case that triggers this can be found here:
https://github.com/PrivateBin/PrivateBin/blob/master/tst/ModelTest.php#L317-L325

And here is some background on this deprecation and various ways to address this:
https://php.watch/versions/8.2/dynamic-properties-deprecated

I'm happy to provide you a pull request, if you could review the solutions and pick an appropriate one for this project.

Performance considerations

In PrivateBin/PrivateBin#1008 we tried using this as a replacement of the deprecated yzalis/identicon library, but we had to switch back to it given that the performance cost was high.

Quoting the above text:

Jdenticon takes between 9 (PHP 8.1) and 14 (PHP 7.4) times as long to generate the PNGs over Identicon and they are about double the size, but still a little smaller then vizhash's.

(Benchmark results are included there.)

You

offer optional ImageMagick support and will autodetect and enable it in PHP 5, but don't recommend it for PHP 7

As such, I see no option to gain speed here when this library is used.

Questions

  • Can we improve performance here?
  • One option may be to include GD autodetection/support, if that is installed? Maybe that would be an option

Note that I do not want to imply Imagemagick support should be enabled for later PHP versions given it's bad security track record as popular PHP software also shows, but maybe we can find a different solution here.


@elrido please correct me if I said something wrong here.

PHP Info at Canvas/Rasterization/EdgeTable.php

Line 61 should look like this:
return (isset($this->scanlines[$y]) ? $this->scanlines[$y] : null);

otherwise PHP Info is triggered sometimes even if the original code starts with "@"

Couldn't generate png image

I am trying to generate png images, but failing with the following exception.

ImagickException
unable to open file `/tmp/magick-64T_RFaCmhklN': No such file or directory @ error/constitute.c/ReadImage/544


in ImagickRenderer.php line 97
--
at Imagick->readimageblob('<?xml version="1.0" encoding="UTF-8" ?><svg xmlns="http://www.w3.org/2000/svg" width="100.00" height="100.00" viewBox="0 0 100.00 100.00" preserveAspectRatio="xMidYMid meet"><rect fill="#ffffff" fill-opacity="1.00" x="0" y="0" width="100.00" height="100.00"/><path fill="#545454" d="M32.50 18.50a7.00,7.00 0 1,1 14.00,0a7.00,7.00 0 1,1 -14.00,0M53.50 18.50a7.00,7.00 0 1,1 14.00,0a7.00,7.00 0 1,1 -14.00,0M53.50 81.50a7.00,7.00 0 1,1 14.00,0a7.00,7.00 0 1,1 -14.00,0M32.50 81.50a7.00,7.00 0 1,1 14.00,0a7.00,7.00 0 1,1 -14.00,0M11.50 39.50a7.00,7.00 0 1,1 14.00,0a7.00,7.00 0 1,1 -14.00,0M74.50 39.50a7.00,7.00 0 1,1 14.00,0a7.00,7.00 0 1,1 -14.00,0M74.50 60.50a7.00,7.00 0 1,1 14.00,0a7.00,7.00 0 1,1 -14.00,0M11.50 60.50a7.00,7.00 0 1,1 14.00,0a7.00,7.00 0 1,1 -14.00,0"/><path fill="#d1757a" d="M8.00 18.50L18.50 8.00L29.00 18.50L18.50 29.00ZM81.50 8.00L92.00 18.50L81.50 29.00L71.00 18.50ZM92.00 81.50L81.50 92.00L71.00 81.50L81.50 71.00ZM18.50 92.00L8.00 81.50L18.50 71.00L29.00 81.50ZM29.00 29.00L50.00 29.00L50.00 50.00L29.00 50.00ZM36.56 42.02a5.46,5.46 0 1,0 10.92,0a5.46,5.46 0 1,0 -10.92,0M71.00 29.00L71.00 50.00L50.00 50.00L50.00 29.00ZM52.52 42.02a5.46,5.46 0 1,0 10.92,0a5.46,5.46 0 1,0 -10.92,0M71.00 71.00L50.00 71.00L50.00 50.00L71.00 50.00ZM52.52 57.98a5.46,5.46 0 1,0 10.92,0a5.46,5.46 0 1,0 -10.92,0M29.00 71.00L29.00 50.00L50.00 50.00L50.00 71.00ZM36.56 57.98a5.46,5.46 0 1,0 10.92,0a5.46,5.46 0 1,0 -10.92,0"/></svg>')in ImagickRenderer.php line 97

But i can able to generate svg images. Any idea how to resolve this issue would be helpful?

PHP Info (low warning) cause of missing Index

Canvas/Png/PngEncode.php Line 144 produces "Undefined offset" under some rare circumstances

Reproduce:

$ident  = new Jdenticon\Identicon(
    array(
        'style' => array(
            'backgroundColor' => '#FFFFFF00',
            'colorLightness' => array(0.00, 0.42),
            'grayscaleLightness' => array(0.00, 0.42),
            'colorSaturation' => 0.00,
            'grayscaleSaturation' => 0.00,
        ))
);
$ident->setValue('PR59358533')->setSize(500);
$ident->getImageData('png');

my temporary (?) fix:
$colorIndex = ((isset($palette->lookup[$value])) ? $palette->lookup[$value] : 0);

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.