How to build a PHP image viewer

Table of contents

    How to build a PHP image viewer
    TL;DR

    Build a PHP image viewer using Nutrient Web SDK: Download the SDK, copy assets to your project, and initialize with NutrientViewer.load(). View JPEG, PNG, and TIFF files directly in the browser.

    This post provides a step-by-step guide for deploying a PHP image viewer with Nutrient Web SDK.

    What is a PHP image viewer?

    A PHP image viewer uses PHP to display image documents in a web browser without downloading them to your hard drive or using an external application.

    Nutrient PHP image viewer

    We offer a commercial PHP image viewer library that integrates into your web application. Our PHP viewer supports rendering JPEG, PNG, and TIFF files in any modern browser and on any mobile device without plugins.

    It comes with 30+ features that let you view, annotate, edit, and sign documents directly in your browser. Out of the box, it has a polished and flexible UI that you can extend or simplify based on your unique use case.

    • A prebuilt and polished UI for an improved user experience
    • 15+ prebuilt annotation tools to enable document collaboration
    • Support for more file types with client-side PDF, MS Office, and image viewing
    • Dedicated support from engineers to speed up integration

    Example of our PHP image viewer

    To see our image viewer in action, upload a JPG, PNG, or TIFF file by selecting Choose Example > Open Document. Once your image is displayed in the viewer, you can try drawing freehand, adding a note, or applying a crop or an eSignature.

    Requirements to get started

    To get started, you’ll need:

    • PHP

    You can install PHP via XAMPP(opens in a new tab), MAMP(opens in a new tab), or Homebrew(opens in a new tab).

    Adding Nutrient to your project

    1. Download the SDK from your Nutrient dashboard(opens in a new tab). The download will save a .tar.gz archive to your computer.
    2. Extract the archive and copy the contents of the dist folder to your project’s assets folder.

    Your assets directory should contain:

    • nutrient-viewer.js
    • nutrient-viewer-lib/ (fonts, WebAssembly files, etc.)

    Integrating into your project

    1. Add the image you want to display to your project’s directory. You can use our demo image as an example.

    2. Add an empty <div> element with a defined height where the viewer will be mounted:

      <div id="nutrient" style="height: 100vh;"></div>
    3. Include nutrient-viewer.js in your PHP page:

      <script src="assets/nutrient-viewer.js"></script>
    4. Initialize Nutrient Web SDK in PHP by calling NutrientViewer.load():

    <script>
    const baseUrl = `${window.location.protocol}//${window.location.host}/assets/`;
    NutrientViewer.load({
    baseUrl,
    container: "#nutrient",
    document: "image.png" // Add the path to your image here.
    })
    .then(function(instance) {
    console.log("Nutrient loaded", instance);
    })
    .catch(function(error) {
    console.error(error.message);
    });
    </script>

    You can see the full index.php file (which is just a plain HTML file):

    <!DOCTYPE html>
    <html>
    <head>
    <title>My App</title>
    <!-- Provide proper viewport information so that the layout works on mobile devices. -->
    <meta
    name="viewport"
    content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"
    />
    </head>
    <body>
    <!-- Element where the viewer will be mounted. -->
    <div id="nutrient" style="height: 100vh;"></div>
    <script src="assets/nutrient-viewer.js"></script>
    <script>
    const baseUrl = `${window.location.protocol}//${window.location.host}/assets/`;
    NutrientViewer.load({
    baseUrl,
    container: '#nutrient',
    document: 'image.png', // Add the path to the image here.
    })
    .then(function (instance) {
    console.log('Nutrient loaded', instance);
    })
    .catch(function (error) {
    console.error(error.message);
    });
    </script>
    </body>
    </html>

    Serving your website

    1. Go to your terminal and run a server with this command:

      Terminal window
      php -S 127.0.0.1:8000
    2. Navigate to http://127.0.0.1:8000 to view the website.

    PHP image viewer displaying an image in the browser

    Adding more capabilities

    Once you’ve deployed your viewer, you can customize it or add more capabilities. Here are some popular PHP guides:

    Conclusion

    You now have a working PHP image viewer. For questions, contact our Support team.

    You can also integrate our JavaScript image viewer or our JavaScript image annotation library using frameworks like Angular and React.js. Start a free trial or try the demo.

    FAQ

    What is a PHP image viewer?

    A PHP image viewer displays image files (JPEG, PNG, TIFF) directly in a web browser without downloading them or using an external application. It uses PHP for server-side functionality and JavaScript for client-side rendering.

    How do I integrate Nutrient into a PHP project?

    Download the Nutrient Web SDK, extract the dist folder contents to your project’s assets directory, include nutrient-viewer.js in your PHP page, and initialize the viewer using NutrientViewer.load().

    What image formats does Nutrient support?

    Nutrient supports JPEG, PNG, and TIFF image formats. It renders these files directly in the browser without requiring plugins or external software.

    Can I add annotations to images with Nutrient?

    Yes. Nutrient includes 15+ annotation tools for adding freehand drawings, notes, text, shapes, and signatures to images directly in the viewer.

    Does Nutrient require a backend server?

    No. Nutrient renders images client-side in the browser. You only need a web server to serve the PHP page and static files.

    Hulya Masharipov

    Hulya Masharipov

    Technical Writer

    Hulya is a frontend web developer and technical writer who enjoys creating responsive, scalable, and maintainable web experiences. She’s passionate about open source, web accessibility, cybersecurity privacy, and blockchain.

    Explore related topics

    Try for free Ready to get started?