How i can resize Images with imagescale in PHP - php

i tried to resize an image with following method:
code
$thumbimg = $img->resizeImage($thumb, 200, 200);
public function resizeImage($imagePath,$new_width,$new_height) {
$fileName = pathinfo($imagePath,PATHINFO_FILENAME);
$ext = pathinfo($imagePath,PATHINFO_EXTENSION);
$fullPath = pathinfo($imagePath,PATHINFO_DIRNAME)."/".$fileName.'.'.$ext;
if (file_exists($fullPath)) {
return $fullPath;
}
$image = $this->openImage($imagePath);
if ($image == false) {
return null;
}
$width = imagesx($image);
$height = imagesy($image);
$imageResized = imagecreatetruecolor($width, $height);
if ($imageResized == false) {
return null;
}
$image = imagecreatetruecolor($width , $height);
$imageResized = imagescale($image,$new_width,$new_height);
touch($fullPath);
$write = imagepng($imageResized,$fullPath);
if (!$write) {
imagedestroy($imageResized);
return null;
}
imagedestroy($imageResized);
return $fullPath;
}
Actual its saves my images in the original width and height. But why? Whats wrong?

If $imagePath refers to an existing image, then $fullPath will refer to that same file. So either the file exists, in which case it is returned, or the file does not exist, in which case you can't open the image and null is returned. Either way, the image resize code is not used.

Related

How to upload an image and create a thumbnail at the same time in PHP?

I'm going to create an image gallery website. I need to resize and crop the uploaded image to create a thumbnail for each image. I used these codes:
cropimage.php:
<?php
class Crop
{
public $img_name;
public $tmp_img_name;
public $folder;
public $ext;
public $new_name;
function CropImage($file, $max_resolution)
{
if (file_exists($file)) {
$original_image = imagecreatefromjpeg($file);
$original_width = imagesx($original_image);
$original_height = imagesy($original_image);
//Try max-width first
if ($original_height > $original_width) {
$ratio = $max_resolution / $original_width;
$new_width = $max_resolution;
$new_height = $original_height * $ratio;
} else {
$ratio = $max_resolution / $original_height;
$new_height = $max_resolution;
$new_width = $original_width * $ratio;
}
if ($original_image) {
$new_image = imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($new_image, $original_image, 0, 0, 0, 0, $new_width, $new_height, $original_width, $original_height);
$new_crop_image = imagecreatetruecolor($max_resolution, $max_resolution);
imagecopyresampled($new_crop_image, $new_image, 0, 0, 0, 0, $max_resolution, $max_resolution, $max_resolution, $max_resolution);
imagejpeg($new_crop_image, $file, 90);
}
}
}
public function RunCrop()
{
$thumb_name_name = $this->img_name;
$thumb_tmp_name = $this->tmp_img_name;
$thumb_new_name = $this->new_name;
$thumb_folder = "../public/assets/uploadThumb/";
$file = $thumb_folder . $thumb_new_name;
//Copy the original image to thumb folder
copy($this->folder . $this->new_name, $file);
//Resize file
$this->CropImage($file, "300");
}
}
saveimage.php:
<?php
include "../app/core/config.php";
include "../app/core/cropImage.php";
class UploadImage
{
private $ext = "";
private $new_name = "";
private $save_result = 0;
public function UploadIMG()
{
if (isset($_POST['img_submit'])) {
try {
//For uploading original image
$crop = new Crop();
$crop->img_name = $_FILES['image']['name'];
$crop->tmp_img_name = $_FILES['image']['tmp_name'];
$crop->folder = "../public/assets/img/";
//For finding file extension
$crop->ext = pathinfo($crop->img_name, PATHINFO_EXTENSION);
//Renaming the uploaded file
$crop->new_name = $this->RandomStringGenerator() . "." . $crop->ext;
$this->new_name = $crop->new_name;
//Moving to the desired path
move_uploaded_file($crop->tmp_img_name, $crop->folder . $crop->new_name);
$this->RegisterIntoDatabase();
//For cropping image and creating thumbnail
$crop->RunCrop();
unset($_POST);
$this->save_result = 1;
return $this->save_result;
} catch (\Throwable $th) {
$this->save_result = 2;
return $this->save_result;
}
}
}
public function RandomStringGenerator()
{
$permitted_chars = "0123456789abcdefghijkl";
$random_string = substr(str_shuffle($permitted_chars), 0, 10);
return $random_string;
}
public function RegisterIntoDatabase()
{
require "../app/core/database.php";
$bold_title = $_POST['boldtitle'];
$title = $_POST['title'];
$subtitle = $_POST['subtitle'];
$image = $this->new_name;
$sql = "INSERT INTO images (title_1, title_2, subtitle, image, thumb)
VALUES ('$bold_title', '$title', '$subtitle', '$image', '$image')";
$connection->query($sql);
}
}
The problem is that RandomStringGenerator() function is run more than once and generates two different image file name and cause the RunCrop() function to fail, because it cannot find that specific file name in the path. How can I fix it?
Enabling php GD in XAMPP setting, solved my problem.

How to set aspect ratio in below code using PHP

I tried 2 implementations, shown below. Both are working well but when I tried to mix them it is not working anymore.
$output['status']=FALSE;
set_time_limit(0);
$allowedImageType = array("image/gif", "image/jpeg", "image/pjpeg", "image/png", "image/x-png" );
if ($_FILES['image_file_input']["error"] > 0) {
$output['error']= "File Error";
}
elseif (!in_array($_FILES['image_file_input']["type"], $allowedImageType)) {
$output['error']= "Invalid image format";
}
elseif (round($_FILES['image_file_input']["size"] / 1024) > 4096) {
$output['error']= "Maximum file upload size is exceeded";
} else {
$temp_path = $_FILES['image_file_input']['tmp_name'];
$file = pathinfo($_FILES['image_file_input']['name']);
$fileType = $file["extension"];
$photo_name = $productname.'-'.$member_id."_".time();
$fileName1 = $photo_name . '-125x125' . ".jpg";
$fileName2 = $photo_name . '-250x250' . ".jpg";
$fileName3 = $photo_name . '-500x500' . ".jpg";
$small_thumbnail_path = "uploads/large/";
createFolder($small_thumbnail_path);
$small_thumbnail = $small_thumbnail_path . $fileName1;
$medium_thumbnail_path = "uploads/large/";
createFolder($medium_thumbnail_path);
$medium_thumbnail = $medium_thumbnail_path . $fileName2;
$large_thumbnail_path = "uploads/large/";
createFolder($large_thumbnail_path);
$large_thumbnail = $large_thumbnail_path . $fileName3;
$thumb1 = createThumbnail($temp_path, $small_thumbnail,$fileType, 125, 125 );
$thumb2 = createThumbnail($temp_path, $medium_thumbnail, $fileType, 250, 250);
$thumb3 = createThumbnail($temp_path, $large_thumbnail,$fileType, 500, 500);
if($thumb1 && $thumb2 && $thumb3) {
$output['status']=TRUE;
$output['small']= $small_thumbnail;
$output['medium']= $medium_thumbnail;
$output['large']= $large_thumbnail;
}
}
echo json_encode($output);
Function File
function createFolder($path)
{
if (!file_exists($path)) {
mkdir($path, 0755, TRUE);
}
}
function createThumbnail($sourcePath, $targetPath, $file_type, $thumbWidth, $thumbHeight){
$source = imagecreatefromjpeg($sourcePath);
$width = imagesx($source);
$height = imagesy($source);
$tnumbImage = imagecreatetruecolor($thumbWidth, $thumbHeight);
imagecopyresampled($tnumbImage, $source, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $width, $height);
if (imagejpeg($tnumbImage, $targetPath, 90)) {
imagedestroy($tnumbImage);
imagedestroy($source);
return TRUE;
} else {
return FALSE;
}
}
Aspect ratio code, which is another one I tried to mix this code. But I was unsuccessful
$fn = $_FILES['image']['tmp_name'];
$size = getimagesize($fn);
$ratio = $size[0]/$size[1]; // width/height
$photo_name = $productname.'-'.$member_id."_".time();
{
if( $ratio > 1) {
$width1 = 500;
$height1 = 500/$ratio;
}
else {
$width1 = 500*$ratio;
$height1 = 500;
}
$src = imagecreatefromstring(file_get_contents($fn));
$dst = imagecreatetruecolor($width1,$height1);
$fileName3 = $photo_name . '-500x500' . ".jpg";
imagecopyresampled($dst,$src,0,0,0,0,$width1,$height1,$size[0],$size[1]);
imagedestroy($src);
imagepng($dst,$fileName3); // adjust format as needed
imagedestroy($dst);
if( $ratio > 1) {
$width2 = 250;
$height2 = 250/$ratio;
}
else {
$width2 = 250*$ratio;
$height2 = 250;
}
$src = imagecreatefromstring(file_get_contents($fn));
$dst = imagecreatetruecolor($width2,$height2);
$fileName2 = $photo_name . '-250x250' . ".jpg";
imagecopyresampled($dst,$src,0,0,0,0,$width2,$height2,$size[0],$size[1]);
imagedestroy($src);
imagepng($dst,$fileName2); // adjust format as needed
imagedestroy($dst);
}
What I need is to save my image after resizing but in second code there is no condition check, and I can't get image upload folder path. That's why I need to merge these 2 codes.
Basically I need need to save my image in 3 size formats: 500x500,250x250 and 125x125. Width is fixed, but height is set as per aspect ratio and set upload folder and condition in second code block.
Try this thumbnail function, which takes your source image resource and thumbnail size, and returns a new image resource for the thumbnail.
function createThumbnail($src, int $width = 100, int $height = null) {
// Ensure that src is a valid gd resource
if (!(is_resource($src) && 'gd' === get_resource_type($src))) {
throw new InvalidArgumentException(
sprintf("Argument 1 of %s expected type resource(gd), %s supplied", __FUNCTION__, gettype($src))
);
}
// Extract source image width, height and aspect ratio
$source_width = imagesx($src);
$source_height = imagesy($src);
$ratio = $source_width / $source_height;
// We know that width is always supplied, and that height can be null
// We must solve height according to aspect ratio if null is supplied
if (null === $height) {
$height = round($width / $ratio);
}
// Create the thumbnail resampled resource
$thumb = imagecreatetruecolor($width, $height);
imagecopyresampled($thumb, $src, 0, 0, 0, 0, $width, $height, $source_width, $source_height);
return $thumb;
}
Given your code above, you can now use the function like this
// Get uploaded file information as you have
// Create your source resource as you have
$source = imagecreatefromstring(file_get_contents($fn));
// Create thumbnails and save + destroy
$thumb = createThumbnail($source, 500);
imagejpeg($thumb, $thumb500TargetPath, 90);
imagedestroy($thumb);
$thumb = createThumbnail($source, 250);
imagejpeg($thumb, $thumb250TargetPath, 90);
imagedestroy($thumb);
$thumb = createThumbnail($source, 125);
imagejpeg($thumb, $thumb125TargetPath, 90);
imagedestroy($thumb);
// Don't forget to destroy the source resource
imagedestroy($source);

PHP image uploads - moving from one type to many

Currently I'm able to upload jpg files, and I'm saving them as jpg files. Obviously I'm unable to upload/save png files as it's hard-set to jpg.
I'm trying to work out how I can move from jpg only to allow png, jpg and jpeg.
public static function createAvatar()
{
// check if upload fits all rules
AvatarModel::validateImageFile();
// create a jpg file in the avatar folder, write marker to database
$target_file_path = Config::get('PATH_AVATARS') . Session::get('user_id');
AvatarModel::resizeAvatarImage($_FILES['avatar_file']['tmp_name'], $target_file_path, Config::get('AVATAR_SIZE'), Config::get('AVATAR_SIZE'), Config::get('AVATAR_JPEG_QUALITY'));
AvatarModel::writeAvatarToDatabase(Session::get('user_id'));
Session::set('user_avatar_file', AvatarModel::getPublicUserAvatarFilePathByUserId(Session::get('user_id')));
Session::add('feedback_positive', Text::get('FEEDBACK_AVATAR_UPLOAD_SUCCESSFUL'));
return true;
}
public static function resizeAvatarImage($source_image, $destination, $final_width = 130, $final_height = 130, $quality = 8)
{
list($width, $height) = getimagesize($source_image);
if (!$width || !$height) {
return false;
}
//saving the image into memory (for manipulation with GD Library)
$myImage = imagecreatefromjpeg($source_image);
// calculating the part of the image to use for thumbnail
if ($width > $height) {
$y = 0;
$x = ($width - $height) / 2;
$smallestSide = $height;
} else {
$x = 0;
$y = ($height - $width) / 2;
$smallestSide = $width;
}
// copying the part into thumbnail, maybe edit this for square avatars
$thumb = imagecreatetruecolor($final_width, $final_height);
imagecopyresampled($thumb, $myImage, 0, 0, $x, $y, $final_width, $final_height, $smallestSide, $smallestSide);
// add '.jpg' to file path, save it as a .jpg file with our $destination_filename parameter
$destination .= '.jpg';
imagejpeg($thumb, $destination, $quality);
// delete "working copy"
imagedestroy($thumb);
if (file_exists($destination)) {
return true;
}
// default return
return false;
}
public static function writeAvatarToDatabase($user_id)
{
$database = DatabaseFactory::getFactory()->getConnection();
$query = $database->prepare("UPDATE users SET user_has_avatar = TRUE WHERE user_id = :user_id LIMIT 1");
$query->execute(array(':user_id' => $user_id));
}
This particular part is where the issue lies
$destination .= '.jpg';
imagejpeg($thumb, $destination, $quality);
I've tried adding a switch on the file type and then doing imagejpeg/png/jpg(,,,) depending which filetype the file has and it didn't work as it seemed to be trying to pass a .tmp file.
Any ideas?
You will want to create the image from the beginning as the intended file. Here is a class I use, and then added into your class. You can copy from the one class to the other, but you can see where you need to change things at least:
class AvatarModel
{
public static function resizeAvatarImage($source_image, $destination, $final_width = 130, $final_height = 130, $quality = 8)
{
// Initiate class
$ImageMaker = new ImageFactory();
// Here is just a test landscape sized image
$source_image = 'http://media1.santabanta.com/full6/Outdoors/Landscapes/landscapes-246a.jpg';
// This will save the file to disk. $destination is where the file will save and with what name
// $destination = 'image60px.jpg';
// $ImageMaker->Thumbnailer($source_image,$final_width,$final_height,$destination,$quality);
// This example will just display to browser, not save to disk
$ImageMaker->Thumbnailer($source_image,$final_width,$final_height,false,$quality);
}
}
class ImageFactory
{
public $destination;
protected $original;
public function FetchOriginal($file)
{
$size = getimagesize($file);
$this->original['width'] = $size[0];
$this->original['height'] = $size[1];
$this->original['type'] = $size['mime'];
return $this;
}
public function Thumbnailer($thumb_target = '', $width = 60,$height = 60,$SetFileName = false, $quality = 80)
{
// Set original file settings
$this->FetchOriginal($thumb_target);
// Determine kind to extract from
if($this->original['type'] == 'image/gif')
$thumb_img = imagecreatefromgif($thumb_target);
elseif($this->original['type'] == 'image/png') {
$thumb_img = imagecreatefrompng($thumb_target);
$quality = 7;
}
elseif($this->original['type'] == 'image/jpeg')
$thumb_img = imagecreatefromjpeg($thumb_target);
else
return false;
// Assign variables for calculations
$w = $this->original['width'];
$h = $this->original['height'];
// Calculate proportional height/width
if($w > $h) {
$new_height = $height;
$new_width = floor($w * ($new_height / $h));
$crop_x = ceil(($w - $h) / 2);
$crop_y = 0;
}
else {
$new_width = $width;
$new_height = floor( $h * ( $new_width / $w ));
$crop_x = 0;
$crop_y = ceil(($h - $w) / 2);
}
// New image
$tmp_img = imagecreatetruecolor($width,$height);
// Copy/crop action
imagecopyresampled($tmp_img, $thumb_img, 0, 0, $crop_x, $crop_y, $new_width, $new_height, $w, $h);
// If false, send browser header for output to browser window
if($SetFileName == false)
header('Content-Type: '.$this->original['type']);
// Output proper image type
if($this->original['type'] == 'image/gif')
imagegif($tmp_img);
elseif($this->original['type'] == 'image/png')
($SetFileName !== false)? imagepng($tmp_img, $SetFileName, $quality) : imagepng($tmp_img);
elseif($this->original['type'] == 'image/jpeg')
($SetFileName !== false)? imagejpeg($tmp_img, $SetFileName, $quality) : imagejpeg($tmp_img);
// Destroy set images
if(isset($thumb_img))
imagedestroy($thumb_img);
// Destroy image
if(isset($tmp_img))
imagedestroy($tmp_img);
}
}
AvatarModel::resizeAvatarImage();

Why does he method imagecreatefromjpeg($src) return false in my method?

I am new to php and I am trying to make thumbnails
$src is the path to the image
$thumbWidth is the desired width
$imageName is not important it is needed to be passed to a function that generates the html code for the thumbnail.
the problem is on line 174 which I commented out if the image is a jpeg file the function returns false and then $source_image is false can anyone explain why?
here is my method:
function makeThumb( $src, $thumbWidth, $imageName )
{
$count = 0;
$len = strlen($src);
$indexlen = $len - 1;
$sourceArray = str_split($src);
for($i = $indexlen; $i > -1; $i--)
{
if($sourceArray[$i] == '.')
{
$count = $count + 1;
if($count == 1)
{
$hold = $i;
}
}
}
$ending = substr($src, $hold, $len);
if($ending === '.gif')
{
$type = '.gif';
$source_image = imagecreatefromgif($src);
}
if($ending === '.jpeg' || $ending === '.pjpeg' || $ending === '.jpg')
{
$type = '.jpg';
$source_image = imagecreatefromjpeg($src);
}
if($ending === '.png')
{
$type = '.png';
$source_image = imagecreatefrompng($src);
}
else
{
//throw new Exception('This file is not in JPG, GIF, or PNG format!');
$type = null;
}
/* read the source image */
if($ending = null)
{ return null; }
$width = imagesx($src);
$height = imagesy($src);
$newWidth = $thumbWidth;
/* find the "desired height" of this thumbnail, relative to the desired width */
$newHeight = floor($height * ($newWidth / $width));
/* create a new, "virtual" image */
$tempImg = imagecreatetruecolor($desired_width, $desired_height);
$pic = formatImage($tempImg, $imageName);
return $pic;
/* copy source image at a resized size */
//imagecopyresampled($virtual_image, $source_image, 0, 0, 0, 0, $desired_width, $desired_height, $width, $height);
/* create the physical thumbnail image to its destination */
//imagejpeg($virtual_image, $dest);
}
Are you sure that the image is a valid jpeg image?
You check the type of the image by extension. You can check get the extension in a much simpler way. You should also check if the file exists:
function makeThumb( $src, $thumbWidth, $imageName )
{
if(!file_exists($src))
throw new Exception('The file '.$src.' does not exist.');
$ext = pathinfo($src, PATHINFO_EXTENSION);
But checking the type of an image using extension is not reliable. There is another way to check that using the getimagesize function:
function makeThumb( $src, $thumbWidth, $imageName )
{
if(!file_exists($src))
throw new Exception('The file '.$src.' does not exist.');
$info = getimagesize($src);
if($info === false)
throw new Exception('This file is not a valid image');
$type = $info[2];
$width = $info[0]; // you don't need to use the imagesx and imagesy functions
$height = $info[1];
switch($type) {
case IMAGETYPE_JPEG:
$source_image = imagecreatefromjpeg($src);
break;
case IMAGETYPE_GIF:
$source_image = imagecreatefromgif($src);
break;
case IMAGETYPE_PNG:
$source_image = imagecreatefrompng($src);
break;
default:
throw new Exception('This file is not in JPG, GIF, or PNG format!');
}
$newWidth = $thumbWidth;
// ..and here goes the rest of your code
Please note - I did not check the rest of your code since your problem is related to the first part of the function.

Check image dimensions before upload

Hi I based this function from the one I've found on web and tried modifying it up for my PHP page for uploading of their icons but I want to limit the user from uploading an image which should only size 100x100px. Well I just call it using this:
uploadImage($id,$_FILES['upload']['name'],$_FILES['upload']['tmp_name']);
This is the function I made:
function uploadImage($new_name,$imagename,$tmp_name){
if($tmp_name!=null||$tmp_name!=""){
list($width, $height, $type, $attr) = getimagesize($tmp_name);
if($width==100&&$height==100){
$image1 = $imagename;
$extension = substr($image1, strrpos($image1, '.') + 1);
$image = "$new_name.$extension";
$folder = "Images/";
if($image) {
$filename = $folder.$image;
$copied = copy($tmp_name, $filename);
}
else echo "image not uploaded.";
}
else
echo "upload only 100x100px image!";
}
}
Now the problem is that even if I uploaded an image which exceeds the 100 x 100px dimensions it still proceeds without returning any errors and now I'm lost with it.
You probably need to re-factor your code a bit; have a function that checks whether the uploaded image is valid, and then one actually does the upload. Alternatively, you could create a class.
<?php
class ImageUpload
{
public $tmpImage;
public $maxWidth = 100;
public $maxHeight = 100;
public $errors = [];
public function __construct($image)
{
$this->tmpImage = $image;
}
public function upload()
{
// Check image is valid; if not throw exception
// Check image is within desired dimensions
list($width, $height) = getimagesize($this->tmpImage);
if ($width > $this->maxWidth || $height > $this->maxHeight) {
throw new Exception(sprintf('Your image exceeded the maximum dimensions (%d×%d)', $this->maxWidth, $this->maxHeight));
}
// Create filename
// Do the upload logic, i.e. move_uploaded_file()
}
}
You can then use this class as follows:
<?php
$imageUpload = new ImageUpload($_FILES['upload']['tmp_name']);
try {
$imageUpload->upload();
} catch (Exception $e) {
echo 'An error occurred: ' . $e->getMessage();
}
This was written off the cuff, so they may be errors. But hopefully it demonstrates a better way to handle file uploads, and errors that may occur during upload.
well, you can also resize the image after upload.
function createFixSizeImage( $pathToImages, $pathToFixSizeImages, $Width )
{
// open the directory
$dir = opendir( $pathToImages );
// loop through it, looking for any/all JPG files:
while (false !== ($fname = readdir( $dir ))) {
$image_info = getimagesize( "path/to/images/".$fname );
$image_width = $image_info[0];
$image_height = $image_info[1];
$image_type = $image_info[2];
switch ( $image_type )
{
case IMAGETYPE_JPEG:
// parse path for the extension
$info = pathinfo($pathToImages . $fname);
// continue only if this is a JPEG image
if ( strtolower($info['extension']) == 'jpeg' )
{
// load image and get image size
$img = imagecreatefromjpeg( "{$pathToImages}{$fname}" );
$width = imagesx( $img );
$height = imagesy( $img );
// give the size,u want
$new_width = 100;
$new_height = 100;
// create a new temporary image
$tmp_img = imagecreatetruecolor( $new_width, $new_height );
// copy and resize old image into new image
imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
// save Fix Size Images into a file
imagejpeg( $tmp_img, "{$pathToFixSizeImages}{$fname}" );
}
break;
case IMAGETYPE_PNG:
// parse path for the extension
$info = pathinfo($pathToImages . $fname);
// continue only if this is a JPEG image
if ( strtolower($info['extension']) == 'png' )
{
// load image and get image size
$img = imagecreatefrompng( "{$pathToImages}{$fname}" );
$width = imagesx( $img );
$height = imagesy( $img );
$new_width = 100;
$new_height = 100;
// create a new temporary image
$tmp_img = imagecreatetruecolor( $new_width, $new_height );
// copy and resize old image into new image
imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
// save Fix Size Images into a file
imagejpeg( $tmp_img, "{$pathToFixSizeImages}{$fname}" );
}
break;
case IMAGETYPE_BMP:
echo "bmp";
break;
default:
break;
}
}
}
// close the directory
closedir( $dir );
}
createFixSizeImage("path","path/to/images/to/be/saved",100);
Extending more or less unknown code and then debugging it, is like you wrote some code weeks ago and you don't understand it any longer.
In your case you are extending some existing code (you have not posted the original code, but your wrote that you did it that way) by adding the feature of checking the image size.
So that you do not need to edit much of the (unknown but) working code, create the new feature as a function of it's own:
/**
* #param string $file
* #param int $with
* #param int $height
* #return bool|null true/false if image has that exact size, null on error.
*/
function image_has_size($file, $width, $height)
{
$result = getimagesize($file);
if ($count($result) < 2) {
return null;
}
list($file_width, $file_height) = $result;
return ($file_width == (int) $width)
&& ($file_height == (int) $height);
}
You now have the new functionality in a single function you can much more easily integrate into the original (hopefully otherwise working) code.
Usage:
$imageHasCorrectSize = image_has_size($tmp_name, 100, 100);
So whenever you change code, do it like a surgeon, keeping the cuts as small as possible.

Categories