班级清单
- App\Middleware\Cors
- App\Middleware\Auth
- App\Middleware\Env
- AppMin
- Application
- Route
- Data\Database
- Data\Db2Database
- Data\OdbcDatabase
- Data\Validator
- Data\KeyValue\SqliteStorage
- Data\Log\FileLogger
- Data\Log\HtmlLogger
- Encoding\Base64Url
- Encoding\Json
- Encoding\Utf8
- Environment\DotEnv
- Environment\System
- FileSystem\Search
- FileSystem\Security
- FileSystem\Sync
- Lang\I18N
- Lang\L10N
- Lang\Time
- Media\Image
- Net\Config
- Net\Email
- Net\HttpClient
- Net\HttpResponse
- Net\IP
- Net\SmtpClient
- Security\Crypto
- Security\Password
- Security\Crypto\Encryption
- Security\Crypto\FileEncryption
- Security\Crypto\JWT
- Security\Crypto\PublicKey
- Security\Crypto\Random
- Security\Crypto\SignedData
- Security\Web\CsrfSession
- Security\Web\CsrfStateless
- Security\Web\RateLimit
- Web\Request
- Web\Response
FastSitePHP\Media\Image
Allow an image file to be opened, manipulated, and saved. This class provides basic functionally and is not full featured with filters or advanced features. It is intended to provide needed core functionally when working with image file uploads and for thumbnail generation.
For additional PHP image libraries see the following projects:
源代码
相关链接
- http://image.intervention.io/
- https://kosinix.github.io/grafika/
- https://phpimageworkshop.com/
- https://murze.be/a-package-to-easily-manipulate-images-in-php
- https://github.com/claviska/SimpleImage
- https://github.com/spatie/image-optimizer
- https://github.com/mohuishou/ImageOCR
示例代码
Open and Edit Images Files
// Use the Media Image Class to open an image. If the image is invalid or the
// file extension doesn't match the file type then an exception will be thrown.
// Supported file extensions = [jpg, jpeg, gif, png, webp]
$img = new \FastSitePHP\Media\Image();
$img->open($file_path);
// Generate a Thumbnail or Resize the Image to a specified max width and height.
//
// When both width and height are specified the image will be sized to the
// smaller of the two values so it fits. If only width or only height are
// specified then image will be sized proportionally to the value.
$max_width = 200; // Pixels
$max_height = 200;
$img->resize($max_width, $max_height);
// Images can also be cropped to a specific dimensions.
// This can be used with JavaScript or App cropping libraries to allow users
// to generate thumbnails from a full uploaded image. For example allow
// a user to crop an uploaded image to a profile thumbnail.
$left = 50;
$top = 40;
$width = 120;
$height = 80;
$target_width = $width * 2; // Optional
$target_height = $height * 2; // Optional
$img->crop($left, $top, $width, $height, $target_width, $target_height);
// Images can be rotated which is useful for sites that allow users to upload
// images because images can often upload with incorrect rotation depending on
// the mobile device or a user may simply want to change the rotation.
$degrees = 180;
$img->rotateLeft();
$img->rotateRight();
$img->rotate($degrees);
// Save Quality (0 to 100) can be specified when saving JPG or WEBP images.
// And Compression-Level (0 to 9) can specified when saving PNG files.
$img->saveQuality(90); // Default Quality
$img->pngCompression(6); // Default Compression-Level
// Overwrite an existing image by simply calling [save()] without
// a path or save to a new file by specifying a full file path.
$img->save($save_path);
// Optionally close the image to free memory when finished working with it.
// This happens automatically when the variable is no longer used.
$img->close();
方法
__construct($image_path = null)
Class Constructor - Optionally open an image
__destruct()
Class Destructor - Free memory by closing image when done.
open($image_path)
Open a image file (jpg, png, gif, or webp). Image type is determined by the file extension. If the file contains an invalid image then an E_WARNING error will be triggered causing an ErrorException to be thrown when using the default error setup. Opening a [*.webp] image requires PHP 5.4 or greater.
返回类型: $this
resize($max_width = null, $max_height = null)
Resize an image to a specified max width and height.
When both width and height are specified the image will be sized to the smaller of the two values so it fits.
If only width or only height are specified then image will be sized to that value.
返回类型: $this
crop($left, $top, $width, $height, $target_width = null, $target_height = null)
Resize an image to a specified max width and height.
This can be used with JavaScript or App cropping libraries to allow users to generate thumbnails from a full uploaded image. For example allow a user to crop an uploaded image to a profile thumbnail.
Target Width and Height are optional and can be used to force images to a specific size. For example if you want to allow users to generate a 50x50 profile thumbnail on every crop then use target width and height.
返回类型: $this
rotateLeft()
Rotate an image 90 degrees left (counter-clockwise).
返回类型: $this
rotateRight()
Rotate an image 90 degrees right (clockwise).
返回类型: $this
rotate($degrees)
Rotate an image by the given angle.
返回类型: $this
saveQuality($new_value = null)
Specify the save quality (0 to 100) to be used when saving JPG or WEBP files.
Defaults to 90.
返回类型: int | $this
pngCompression($new_value = null)
Specify the save PNG save compression-level (0 to 9). Defaults to 6.
For more on this topic refer to the links:
返回类型: int | $this
- http://marcjschmidt.de/blog/2013/10/25/php-imagepng-performance-slow.html
- https://tinypng.com/
- https://compresspng.com/
save($file_name = null)
Save the image. If no file name is specified then the name of the opened file will be overwritten otherwise if only a file name is specified then a new file will be saved in the same directory/folder. Additionally full paths can be specified to save to a different directory/folder.
Saving a [*.webp] image requires PHP 5.4 or greater, however using PHP 7 or later is recommended if saving WEBP because saving might silently fail on old versions of PHP.
返回类型: $this
close()
Close Image to free memory