Function: JScms_resize_image
avatar
Señor FAQ

¡Hola, amigos! I’m Señor FAQ, the mustached maestro of questions and answers! With my trusty glasses and a book of endless wisdom, I turn dudas into solutions. Soy el héroe de los curiosos and the champion of clarity.


Function: JScms_resize_image

The JScms_resize_image function resizes and optionally crops images while maintaining aspect ratio or applying center cropping. It also supports watermarking.

Syntax


JScms_resize_image(string $sourcePath, string $targetPath, int $newWidth, int $newHeight, bool $keepRatio = false, ?string $watermarkPath = null, ?int $watermarkPosition = null): bool
    

Parameters

  • $sourcePath: (string) The path to the source image.
  • $targetPath: (string) The path to save the resized image.
  • $newWidth: (int) The desired width of the resized image.
  • $newHeight: (int) The desired height of the resized image.
  • $keepRatio: (bool) Optional. Whether to maintain the original aspect ratio. Default is false.
  • $watermarkPath: (string|null) Optional. Path to the watermark image. Default is null.
  • $watermarkPosition: (int|null) Optional. Position of the watermark based on a 1-9 grid. Default is null (center).

Returns

(bool) Returns true if the image was resized and saved successfully, or false otherwise.

Example Usage


<?php
// Resize an image with aspect ratio and watermark
$result = JScms_resize_image(
    "/path/to/source.jpg",
    "/path/to/target.jpg",
    800,
    600,
    true,
    "/path/to/watermark.png",
    9
);

if ($result) {
    echo "Image resized successfully.";
} else {
    echo "Failed to resize image.";
}
?>
    

Best Practices

  • Ensure the source image exists and is readable.
  • Choose appropriate dimensions for resizing to avoid unnecessary distortion.
  • Use high-quality watermark images in PNG format for best results.

Common Issues

  • Unsupported Image Type: Ensure the source image is in JPEG, PNG, or GIF format.
  • Permission Denied: Verify that the script has permissions to read the source image and write to the target path.
  • Watermark Positioning: Ensure the watermark dimensions are smaller than the resized image dimensions.
Was this article helpful?
0 out of 0 found this helpful