K
K
Kusmich2015-12-02 23:38:27
PHP
Kusmich, 2015-12-02 23:38:27

How to write a condition in php?

I picked up a script that takes images from one folder, reduces it, copies it to another folder. I can't write an if condition that checks if the width of the original image is greater than the height, then the new width = 280, the height is automatically while maintaining proportions;
if the height is greater - the new height is 260, the width is automatically while maintaining the proportions;
here is the whole code:

<?php
//Maximize script execution time
ini_set('max_execution_time', 0);

//Initial settings, Just specify Source and Destination Image folder.
$ImagesDirectory    = 'new1/'; //Source Image Directory End with Slash
$DestImagesDirectory    = 'new2/'; //Destination Image Directory End with Slash
$NewImageWidth ; //New Width of Image
$NewImageHeight; // New Height of Image
$Quality        = 80; //Image Quality



if ($iWidth === $iHeight ){$NewImageWidth = 280;  $NewImageHeight = 280;   };




//Open Source Image directory, loop through each Image and resize it.
if($dir = opendir($ImagesDirectory)){
    while(($file = readdir($dir))!== false){

        $imagePath = $ImagesDirectory.$file;
        $destPath = $DestImagesDirectory.$file;
        $checkValidImage = @getimagesize($imagePath);

        if(file_exists($imagePath) && $checkValidImage) //Continue only if 2 given parameters are true
        {
            //Image looks valid, resize.
            if(resizeImage($imagePath,$destPath,$NewImageWidth,$NewImageHeight,$Quality))
            {
                echo $file.' resize Success!<br />';
                /*
                Now Image is resized, may be save information in database?
                */

            }else{
                echo $file.' resize Failed!<br />';
            }
        }
    }
    closedir($dir);
}

//Function that resizes image.
function resizeImage($SrcImage,$DestImage, $MaxWidth,$MaxHeight,$Quality)
{
    list($iWidth,$iHeight,$type)    = getimagesize($SrcImage);
    $ImageScale             = min($MaxWidth/$iWidth, $MaxHeight/$iHeight);
    $NewWidth               = ceil($ImageScale*$iWidth);
    $NewHeight              = ceil($ImageScale*$iHeight);
    $NewCanves              = imagecreatetruecolor($NewWidth, $NewHeight);

    
    switch(strtolower(image_type_to_mime_type($type)))
{
case 'image/jpeg':
$NewImage = imagecreatefromjpeg($SrcImage);
break;
case 'image/png':
$NewImage = imagecreatefrompng($SrcImage);
break;
case 'image/gif':
$NewImage = imagecreatefromgif($SrcImage);
break;
default:
return false;
}

    // Resize Image
    if(imagecopyresampled($NewCanves, $NewImage,0, 0, 0, 0, $NewWidth, $NewHeight, $iWidth, $iHeight))
    {
        // copy file
        if(imagejpeg($NewCanves,$DestImage,$Quality))
        {
            imagedestroy($NewCanves);
            return true;
        }
    }
}

?>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey, 2015-12-03
@Kusmich

If I understand correctly, you don’t learn how to proportionally calculate the height of images with a larger width and proportionally the width of images with a larger height?

$ratio = $width / $height;
if($width > $height) {
    $width = 280;
    $height = round( $width / $ratio );
}else{
    $height = 260;
    $width = round( $height * $ratio );
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question