PHPackages                             optipic-io/optipic-cdn-php - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Image &amp; Media](/categories/media)
4. /
5. optipic-io/optipic-cdn-php

ActiveLibrary[Image &amp; Media](/categories/media)

optipic-io/optipic-cdn-php
==========================

OptiPic CDN library to convert image urls contains in html/text data

231[1 issues](https://github.com/optipic-io/optipic-cdn-php/issues)PHP

Since Jan 2Pushed 3y ago3 watchersCompare

[ Source](https://github.com/optipic-io/optipic-cdn-php)[ Packagist](https://packagist.org/packages/optipic-io/optipic-cdn-php)[ RSS](/packages/optipic-io-optipic-cdn-php/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

PHP / OptiPic
=============

[](#php--optipic)

Lib for PHP to integrate with CDN OptiPic.io (automatic images optimization and compression service) Use `ImgUrlConverter` class to **automatic convert all image URLs on your site**.

|[English](https://optipic.io/en/cdn/) | [Español](https://optipic.io/es/cdn/) | [Deutsch](https://optipic.io/de/cdn/) | [Türk](https://optipic.io/tr/cdn/) | [Français](https://optipic.io/fr/cdn/) | [Italiano](https://optipic.io/it/cdn/) | [Português](https://optipic.io/pt/cdn/) | [Polski](https://optipic.io/pl/cdn/) | [Čeština](https://optipic.io/cz/cdn/) | [Русский](https://optipic.io/ru/cdn/) | [Беларуская](https://optipic.io/by/cdn/) | [中国](https://optipic.io/cn/cdn/) | [日本](https://optipic.io/jp/cdn/) | [বেঙ্গল](https://optipic.io/bn/cdn/) | [한국인](https://optipic.io/ko/cdn/) |

How to use
----------

[](#how-to-use)

1. [Register](https://optipic.io/register/?cdn) your account on OptiPic.io site.
2. Add your site on OptiPic [CDN Control Panel](https://optipic.io/cdn/cp/).
3. Get your site **ID** from OptiPic CDN Control Panel sites list.
4. Download this lib to your site.
5. Use this library according use cases below

Administration area to install lib and configure it
---------------------------------------------------

[](#administration-area-to-install-lib-and-configure-it)

After downloaded all code of this repository you may open admin area in your browser:

[![cdn optipic admin area](https://camo.githubusercontent.com/65726990cbc160f40a3cfa947135dc913091b96801f5a84c125e2b273fc73b78/68747470733a2f2f63646e2e6f7074697069632e696f2f736974652d3532302f696d672f63646e2f6f7074697069632d63646e2d7068702d61646d696e2d617265612d73637265656e2d312e706e67)](https://camo.githubusercontent.com/65726990cbc160f40a3cfa947135dc913091b96801f5a84c125e2b273fc73b78/68747470733a2f2f63646e2e6f7074697069632e696f2f736974652d3532302f696d672f63646e2f6f7074697069632d63646e2d7068702d61646d696e2d617265612d73637265656e2d312e706e67)

Use case #1: Automatic convert image URLs using 'auto\_prepend\_file' php-directive
-----------------------------------------------------------------------------------

[](#use-case-1-automatic-convert-image-urls-using-auto_prepend_file-php-directive)

Inlude our `/optipic-cdn-php/auto_prepend_file.php` file in `auto_prepend_file` php.ini directive.

You may do it in `.user.ini` or `php.ini` or `.htaccess`.

### Example using `.htaccess`

[](#example-using-htaccess)

```
php_value auto_prepend_file "/optipic-cdn-php/optipic-cdn-php/auto_prepend_file.php"

```

### Example using `php.ini` or `.user.ini`

[](#example-using-phpini-or-userini)

```
; Automatically add files before PHP document.
; http://php.net/auto-prepend-file
auto_prepend_file = /optipic-cdn-php/optipic-cdn-php/auto_prepend_file.php

```

### Hosting limitations to using `auto_prepend_file` php-directive

[](#hosting-limitations-to-using-auto_prepend_file-php-directive)

Some hostings allow you to set `auto_prepend_file` only upon request to support.
Examples of such hostings:

- ukraine.com.ua ([https://www.ukraine.com.ua/wiki/hosting/php/auto\_prepend\_file/](https://www.ukraine.com.ua/wiki/hosting/php/auto_prepend_file/))

Use case #2: Automatic convert image URLs using your site's entry point php-script
----------------------------------------------------------------------------------

[](#use-case-2-automatic-convert-image-urls-using-your-sites-entry-point-php-script)

You may include our 1-line converter in on the top of your site's entry point (e.g. `/index.php`).

```
require_once __DIR__.'/optipic-cdn-php/optipic-cdn-php/ImgUrlConverter.php';

ob_start(array('\optipic\cdn\ImgUrlConverter', 'convertHtml'));

....

....
```

Use case #3: Manually using convert function of our lib in your code
--------------------------------------------------------------------

[](#use-case-3-manually-using-convert-function-of-our-lib-in-your-code)

You may use our lib to convert image URLs exactly where you want in your project to convert whole HTML-block contains image URLs.

```
$converterOptiPic = new \optipic\cdn\ImgUrlConverter(array(
    'site_id' => 99999999,                                       // your SITE ID from CDN OptiPic controll panel
    'domains' => array('mydomain.com', 'www.mydomain.com'),      // list of domains should replace to cdn.optipic.io
    'exclusions_url' => array('/test/test/index.php',),          // list of URL exclusions - where is URL should not converted
    'whitelist_img_urls' => array(),                             // whitelist of images URL - what should to be converted (parts or full urls start from '/')
    'srcset_attrs' => array('srcset', 'data-srcset'),            // tag's srcset attributes // @see https://developer.mozilla.org/en-US/docs/Learn/HTML/Multimedia_and_embedding/Responsive_images
));

$htmlConverted = $converterOptiPic->convertHtml($html);
```

Configuration
-------------

[](#configuration)

### Option #1: Using file config.php in lib's root

[](#option-1-using-file-configphp-in-libs-root)

File format:

```
return array(
    'site_id' => '0',                // your SITE ID from CDN OptiPic controll panel
    'domains' => array(),            // list of domains should replace to cdn.optipic.io
    'exclusions_url' => array(),     // list of URL exclusions - where is URL should not converted
    'whitelist_img_urls' => array(), // whitelist of images URL - what should to be converted (parts or full urls start from '/')
    'srcset_attrs' => array('srcset', 'data-srcset'), // tag's srcset attributes // @see https://developer.mozilla.org/en-US/docs/Learn/HTML/Multimedia_and_embedding/Responsive_images
);
```

In this case config will be automatic loaded in `auto_prepend_file.php`.

### Option #2: Using ImgUrlConverter::loadConfig();

[](#option-2-using-imgurlconverterloadconfig)

```
// Autoload config from config.php
ImgUrlConverter::loadConfig();

// Load config from custom config file
ImgUrlConverter::loadConfig('');

// Load config from array
ImgUrlConverter::loadConfig(array(
    'site_id' => '0',                // your SITE ID from CDN OptiPic controll panel
    'domains' => array(),            // list of domains should replace to cdn.optipic.io
    'exclusions_url' => array(),     // list of URL exclusions - where is URL should not converted
    'whitelist_img_urls' => array(), // whitelist of images URL - what should to be converted (parts or full urls start from '/')
    'srcset_attrs' => array('srcset', 'data-srcset'), // tag's srcset attributes // @see https://developer.mozilla.org/en-US/docs/Learn/HTML/Multimedia_and_embedding/Responsive_images
));
```

### Option #3: Pass config data into ImgUrlConverter::\_\_constructor();

[](#option-3-pass-config-data-into-imgurlconverter__constructor)

```
$converterOptiPic = new \optipic\cdn\ImgUrlConverter(array(
    'site_id' => 99999999,                                       // your SITE ID from CDN OptiPic controll panel
    'domains' => array('mydomain.com', 'www.mydomain.com'),      // list of domains should replace to cdn.optipic.io
    'exclusions_url' => array('/test/test/index.php',),          // list of URL exclusions - where is URL should not converted
    'whitelist_img_urls' => array(),                             // whitelist of images URL - what should to be converted (parts or full urls start from '/')
));
```

### Option #1: Using file config.php in lib's root

[](#option-1-using-file-configphp-in-libs-root-1)

What will be converted (examples)
---------------------------------

[](#what-will-be-converted-examples)

```
 ---CONVERT-->
 ---CONVERT-->
 ---CONVERT-->
backhround: url(/foo/bar/img.jpg) ---CONVERT--> backhround: url(//cdn.optipic.io/site-99999999/foo/bar/img.jpg)
backhround: url("/foo/bar/img.jpg") ---CONVERT--> backhround: url("//cdn.optipic.io/site-99999999/foo/bar/img.jpg")
backhround: url('/foo/bar/img.jpg') ---CONVERT--> backhround: url('//cdn.optipic.io/site-99999999/foo/bar/img.jpg')
 ---CONVERT-->
 ---CONVERT-->
 ---CONVERT-->
backhround: url(http://mydomain.com/foo/bar/img.jpg) ---CONVERT--> backhround: url(//cdn.optipic.io/site-99999999/foo/bar/img.jpg)
backhround: url("http://mydomain.com/foo/bar/img.jpg") ---CONVERT--> backhround: url("//cdn.optipic.io/site-99999999/foo/bar/img.jpg")
backhround: url('http://mydomain.com/foo/bar/img.jpg') ---CONVERT--> backhround: url('//cdn.optipic.io/site-99999999/foo/bar/img.jpg')
 ---CONVERT-->
 ---CONVERT-->
 ---CONVERT-->
backhround: url(https://mydomain.com/foo/bar/img.jpg) ---CONVERT--> backhround: url(//cdn.optipic.io/site-99999999/foo/bar/img.jpg)
backhround: url("https://mydomain.com/foo/bar/img.jpg") ---CONVERT--> backhround: url("//cdn.optipic.io/site-99999999/foo/bar/img.jpg")
backhround: url('https://mydomain.com/foo/bar/img.jpg') ---CONVERT--> backhround: url('//cdn.optipic.io/site-99999999/foo/bar/img.jpg')
 ---CONVERT-->
 ---CONVERT-->
 ---CONVERT-->
backhround: url(https://NOTmydomain.com/foo/bar/img.jpg) ---CONVERT--> backhround: url(https://NOTmydomain.com/foo/bar/img.jpg)
backhround: url("http://NOTmydomain.com/foo/bar/img.jpg") ---CONVERT--> backhround: url("http://NOTmydomain.com/foo/bar/img.jpg")
backhround: url('https://NOTmydomain.com/foo/bar/img.jpg') ---CONVERT--> backhround: url('https://NOTmydomain.com/foo/bar/img.jpg')

```

More live examples see in `/examples/example.php` script.

Where is `php.ini` on different hostings
----------------------------------------

[](#where-is-phpini-on-different-hostings)

### Beget

[](#beget)

`/cgi-bin/php.ini`

### Reg.ru

[](#regru)

If it does not work you also may create/edit file `.user.ini` in the site's root directory - alternative of php.ini file.

`ionCube` Warning
-----------------

[](#ioncube-warning)

If you use ionCube on your site you shouldn't add `auto_prepend_file` direcive in php settings.
The `ionCube` does not support the directive `auto_prepend_file` and [throws an error](https://www.ioncube.com/sa/gui_docs/settings_restrictions.html).
Instead, add the OptiPic connection directly to your site's entry point (usually an index.php) as stated above.

### Example for `ImageCMS` (it's using `ionCube`)

[](#example-for-imagecms-its-using-ioncube)

Into `index.php` insert OptiPic logic before including `core/CodeIgniter.php`

```
// OptiPic
// --------------------------------------------------------------------
if(php_sapi_name() !== 'cli') {
    require_once __DIR__.'/optipic-cdn-php/optipic-cdn-php/ImgUrlConverter.php';
    ob_start(array('\optipic\cdn\ImgUrlConverter', 'convertHtml'));
}
// --------------------------------------------------------------------

/*
 * --------------------------------------------------------------------
 * LOAD THE BOOTSTRAP FILE
 * --------------------------------------------------------------------
 *
 * And away we go...
 *
 */
require_once BASEPATH . 'core/CodeIgniter.php';
```

Which URLs we recommend to exclude based on your CMS
----------------------------------------------------

[](#which-urls-we-recommend-to-exclude-based-on-your-cms)

### Wordpress

[](#wordpress)

`/wp-admin/*`

### Bitrix

[](#bitrix)

`/bitrix/*`

### Joomla

[](#joomla)

`/administrator/*`

### Drupal

[](#drupal)

`/en/admin/*`
`/ru/admin/*`
/&lt;YOUR\_LANG&gt;/admin/\*

### MODX

[](#modx)

`/manager/*`

### OpenCart

[](#opencart)

`/admin/*`

### HostCMS

[](#hostcms)

`/admin/*`

### Other CMS

[](#other-cms)

Exclude your CMS admin area (control panel) folder with mask `*` at the end to exclude all nested urls

###  Health Score

13

—

LowBetter than 1% of packages

Maintenance10

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity23

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/9cc254f466377ef2646a79e30c514cc0187af2500d4d15a1815d4b648339d5b3?d=identicon)[optipic](/maintainers/optipic)

---

Top Contributors

[![optipic-io](https://avatars.githubusercontent.com/u/57090372?v=4)](https://github.com/optipic-io "optipic-io (90 commits)")

---

Tags

compress-imagescompressionconvert-webpgoogle-pagespeed-insightsimage-compressionimage-compressorimages-optimizedminifyoptimizationoptimize-imagespagespeedpagespeed-insightspagespeed-optimizationphpspeed-upspeedupwebp

### Embed Badge

![Health badge](/badges/optipic-io-optipic-cdn-php/health.svg)

```
[![Health](https://phpackages.com/badges/optipic-io-optipic-cdn-php/health.svg)](https://phpackages.com/packages/optipic-io-optipic-cdn-php)
```

###  Alternatives

[milon/barcode

Barcode generator like Qr Code, PDF417, C39, C39+, C39E, C39E+, C93, S25, S25+, I25, I25+, C128, C128A, C128B, C128C, 2-Digits UPC-Based Extention, 5-Digits UPC-Based Extention, EAN 8, EAN 13, UPC-A, UPC-E, MSI (Variation of Plessey code)

1.5k13.3M39](/packages/milon-barcode)[bkwld/croppa

Image thumbnail creation through specially formatted URLs for Laravel

510496.0k22](/packages/bkwld-croppa)[marc1706/fast-image-size

fast-image-size is a PHP library that does almost everything PHP's getimagesize() does but without the large overhead of downloading the complete file.

959.4M20](/packages/marc1706-fast-image-size)[char0n/ffmpeg-php

PHP wrapper for FFmpeg application

495225.1k1](/packages/char0n-ffmpeg-php)[goat1000/svggraph

Generates SVG graphs

132849.6k3](/packages/goat1000-svggraph)[cohensive/embed

Media Embed (for Laravel or as a standalone).

120370.4k](/packages/cohensive-embed)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
