![]() ![]() ![]() only when a new image is uploaded to your server). * the getBytes() call may take a while to run, so it's intended to be called only once per image (e.g. Next, to start the optimizations and get the optimized image, call: $imageData = $imageParams-> getBytes() You set various settings on this object (or none to get the defaults). When you want to generate multiple image sizes/thumbnails, repeat the whole procedure for each image at each size. The API operates on a single image at a time. You don't need to run any PHP code to serve optimized images. Such script usually would be ran when a new image is uploaded to the server. This is a script that optimizes an image. See /api for documentation and contact info. It's best to launch such scripts via CLI (e.g. If you're writing a script that processes a large number of images in one go, don't launch it from a web browser, as it will likely time out. Make sure your server allows access to the file. ImageOptim\OriginServerException is thrown when URL given to imageFromURL() returned 4xx or 5xx error.Make sure paths and urlencoding are correct. ImageOptim\NotFoundException is thrown when URL given to imageFromURL() returned 404.ImageOptim\NetworkException is thrown when there is problem comunicating with the API.ImageOptim\InvalidArgumentException means arguments to functions are incorrect and you need to fix your code.You can expect the following exception subclasses: Error handlingĪll methods throw on error. Or you can make a POST request to it to download the image yourself, if you don't want to use the getBytes() method. You can open this URL in your web browser to get more information about it. Returns string with URL to … that is equivalent of the options set. apiURL() - debug or use another HTTPS client ImageOptim performs optimizations that sometimes may take a few seconds, so instead of converting images on the fly on every request, you should convert them once and keep them. You should save that to your server's disk. Makes request to ImageOptim API and returns optimized image as a string. The default is medium and should be good enough for most cases. Quality is set as a string, and can be low, medium or high. quality($preset) - if you need even smaller or extra sharp images See options reference for explanation how DPR works. This is better than resize($width*2), because it also adjusts sharpness and image quality to be optimal for high-DPI displays. If you set dpr(2) then pixel width and height of the image will be doubled to match density of "2x" displays. The default is dpr(1), which means image is for regular displays, and resize() does the obvious thing you'd expect. dpr($x) - pixel doubling for responsive images (HTML srcset) See options reference for more resizing options. If you don't call resize(), then the original image size will be preserved. Resize($width, $height, 'crop') - resizes and crops image exactly to these dimensions. Resize($width, $height) - same as above, but image will also have height same or smaller. If the image is smaller than this, it won't be enlarged. Resize($width) - sets maximum width for the image, so it'll be resized to this width. resize($width, $height = optional, $fit = optional) - desired dimensions If source images you provide are already saved at low quality, ImageOptim will not be able to make them look better. JPEG saved at 99%), so that ImageOptim can adjust quality itself. Ideally you should supply source image at very high quality (e.g. Please pass full absolute URL to images on your website. published on a website) it's faster to use the URL method instead: imageFromURL($url) - remote source imageĬreates a new request that will read the image from the given public URL, and then resize and optimize it. localhost, files in /tmp).įor images that have a public URLs (e.g. The upload method is necessary for optimizing files that are not on the web (e.g. imageFromPath($filePath) - local source imageĬreates a new request that will upload the image to the API, and then resize and optimize it. Methods API($username) constructor new ImageOptim\API("your api username goes here") Ĭreates new instance of the API. There's a longer example at the end of the readme. > getBytes() // perform these operations and return the image data as binary stringįile_put_contents(" images/photo_optimized.jpg", $imageData) > dpr( 2) // optional: double number of pixels for high-resolution "Retina" displays > resize( 160, 100, 'crop') // optional: resize to a thumbnail If you don't use Composer, then require or autoload files from the src directory. ![]() The easiest is to use PHP Composer: composer require imageoptim/imageoptim ImageOptim offers advanced compression, high-DPI/responsive image mode, and color profile support that are much better than PHP's built-in image resizing functions. This library allows you to resize and optimize images using ImageOptim API. ![]()
0 Comments
Leave a Reply. |