0

I use this image resize class for company's logo resize process:

include 'resize-class.php';

function checkImageReq($image){

    $size = (int) formatBytes(filesize($image)); 

    list($width, $height) = getimagesize($image);

    if($size > 400){
        return FALSE;
    }

    if($width > 300 || $width < 50){
        return FALSE;
    }

    if($height > 300 || $height < 50){
        return FALSE;
    }

    return TRUE;

}


// allows file types
$allowed = array('jpg','png');

// get file type
$extension = pathinfo($_FILES['company_logo']['name'], PATHINFO_EXTENSION);

// I added below methods to create auto resize
$resizeObj = new resize($_FILES['company_logo']['tmp_name']);
$resizeObj -> resizeImage(250, 250, 'auto');
$resizeObj -> saveImage('img.jpg', 1000);


// is there new logo that needs updating
if($_FILES['company_logo']['error'] === 0 && in_array($extension, $allowed)){

    $uploaddir = 'images/logo/';
    $filename = $mainUser->getUserId().'_'.time().'_'.$_FILES['company_logo']['name'];
    $uploadfile = $uploaddir . basename($filename);

    if(checkImageReq($_FILES['company_logo']['tmp_name']) && move_uploaded_file($_FILES['company_logo']['tmp_name'], $uploadfile)){

        // delete old logo
        if(file_exists($uploaddir.$mainUser->getCompanyLogo())){
            unlink($uploaddir.$mainUser->getCompanyLogo());
        }

        $mainUser->setCompanyLogo($filename);

    }else{

        $image_error = 'Error uploading logo.';

        goto end;

    }

when I run this script, I get my new EMPTY image created with width and height 250px, I was suspected $resizeObj = new resize($_FILES['company_logo']['tmp_name']); is never pass the real image to perform auto resize.

Can someone please advise on how to combine this class to my existing code?

Thanks.

Community
  • 1
  • 1
conmen
  • 2,377
  • 18
  • 68
  • 98
  • Maybe this topic would help you out: http://stackoverflow.com/questions/12757005/php-resize-image-on-or-before-upload?lq=1 – Matheno Jul 12 '13 at 09:06

1 Answers1

0

1.First you need to set this php setting:

ini_set('error_reporting', E_ALL | E_STRICT); //to output all errors on screen.

2.Change:

$resizeObj -> saveImage('img.jpg', 1000); // within accepted ranges 0-100

3.You have to set at the initiation of an resize object as a parameter in the constructor the path to the locally stored file, not the temporary one.

ini_set('error_reporting', E_ALL | E_STRICT);
include 'resize-class.php';

function checkImageReq($image){

    $size = (int) formatbytes(filesize($image)); 

    list($width, $height) = getimagesize($image);

    if($size > 400){
        return FALSE;
    }

    if($width > 300 || $width < 50){
        return FALSE;
    }

    if($height > 300 || $height < 50){
        return FALSE;
    }

    return TRUE;

}


// allows file types
$allowed = array('jpg','png');

// get file type
$extension = pathinfo($_FILES['company_logo']['name'], PATHINFO_EXTENSION);


// is there new logo that needs updating
if($_FILES['company_logo']['error'] === 0 && in_array($extension, $allowed)){

    $uploaddir = 'images/logo/';
    $filename = $mainUser->getUserId().time().'_'.$_FILES['company_logo']['name'];
    $uploadfile = $uploaddir . basename($filename);

    if(checkImageReq($_FILES['company_logo']['tmp_name']) && move_uploaded_file($_FILES['company_logo']['tmp_name'], $uploadfile)){
    
        // I added below methods to create auto resize
        $resizeObj = new resize($uploadfile);
        $resizeObj -> resizeImage(250, 250, 'auto');
        $resizeObj -> saveImage($uploaddir. basename("img.png"), 100);
        
        // delete old logo
        if(file_exists($uploaddir.$mainUser->getCompanyLogo())){
            unlink($uploaddir.$mainUser->getCompanyLogo());
        }

        $mainUser->setCompanyLogo($filename);
        
    }else{
        $image_error = 'Error uploading logo.';
        goto end;
    }
}
Community
  • 1
  • 1
TotPeRo
  • 6,561
  • 4
  • 47
  • 60