Jump to content

Help with code to create square images (crop them if needed)


pedromau

Recommended Posts

Hey guys!

I'm having trouble in getting this code to create proportional square images.

 

I just need someone to take a look and tell me what's wrong in there... I'm a damn noob...

A friend of mine gave me that code, but I don't know what to change so it works!

 

Please, help!

 


 

<?php
$storage = 'img';
$today = date("d-m-Y"."-"); 
// full path/filename to save
$uploadfile = "$storage/".$today . basename( $_FILES['Filedata']['name'] );

//$uploadfile = basename( $_FILES['Filedata']['name'] );

 // move the file from the http request to storage
$success = move_uploaded_file( $_FILES['Filedata']['tmp_name'] , $uploadfile );
        
//---resizing uploaded image to a thumb in dir Thumb--- 
  $sizew = 190;$sizeh = 190; // the thumbnail's maximal width and height
  $size2w = 190;$size2h = 190;//the picture's maximal width and height   (requested)
  
    $filedir = 'img/'; // the directory for the original image
$picdir = 'img/'; //the directory for the resized image
    $thumbdir = 'img/'; // the directory for the thumbnail image
    
    $maxfile = '150000';
    $mode = '0666';
    
    $userfile_name = $_FILES['Filedata']['name'];
    $userfile_tmp = $_FILES['Filedata']['tmp_name'];
    $userfile_size = $_FILES['Filedata']['size'];
    $userfile_type = $_FILES['Filedata']['type'];
    
    if (isset($_FILES['Filedata']['name'])) 
    {
        $prod_img = $filedir.$userfile_name;

        $prod_img_thumb = $thumbdir.$userfile_name;
        move_uploaded_file($userfile_tmp, $prod_img);
        chmod ($prod_img, octdec($mode));
        
        $sizes = getimagesize($prod_img);
        
                       
//compare orig w and h   to create thumbs   

        if (($sizes[1]  <= $sizeh) && ($sizes[0] <=$sizew)) 
        {            $new_width = $sizes[0];            $new_height = $sizes[1];        }
        if ($sizes[0] > $sizes[1])        {            $aspect_ratio = $sizes[1]/$sizes[0]; 	            $new_width = $sizew;
            $new_height = abs($new_width*$aspect_ratio);        }        
        else{
            $aspect_ratio = $sizes[0]/$sizes[1]; 
            $new_height = $sizeh;
            $new_width = $sizew; 
		abs($new_height*$aspect_ratio);
        }

        $destimg=ImageCreateTrueColor($new_width,$new_height) or die('Problem In Creating image');
        $srcimg=ImageCreateFromJPEG($prod_img) or die('Problem In opening Source Image');
        ImageCopyResized($destimg,$srcimg,0,0,0,0,$new_width,$new_height,ImageSX($srcimg),ImageSY($srcimg)) or die('Problem In resizing');
        ImageJPEG($destimg,$prod_img_thumb,100) or die('Problem In saving');
        imagedestroy($destimg);
    }
//------added resizing of actual picture-----------------------
if (isset($_FILES['Filedata']['name'])) 
    {
        $prod_img = $filedir.$userfile_name;

        $prod_img_pic = $picdir.$userfile_name;
        move_uploaded_file($userfile_tmp, $prod_img);
        chmod ($prod_img, octdec($mode));
        
        $sizes = getimagesize($prod_img);
        
                       
//compare orig w and h   to create actual picture   

        if (($sizes[1]  <= $size2h) && ($sizes[0] <=$size2w))        {
            $new_width = $sizes[0];            $new_height = $sizes[1];        }
        if ($sizes[0] > $sizes[1])        {            $aspect_ratio = $sizes[1]/$sizes[0]; 	
            $new_width = $size2w;            $new_height = abs($new_width*$aspect_ratio);        }        
        else{
            $aspect_ratio = $sizes[0]/$sizes[1]; 
            $new_height = $size2h;
            $new_width = $size2w; 
		abs($new_height*$aspect_ratio);
        }

        $destimg=ImageCreateTrueColor($new_width,$new_height) or die('Problem In Creating image');
        $srcimg=ImageCreateFromJPEG($prod_img) or die('Problem In opening Source Image');
        ImageCopyResized($destimg,$srcimg,0,0,0,0,$new_width,$new_height,ImageSX($srcimg),ImageSY($srcimg)) or die('Problem In resizing');
        ImageJPEG($destimg,$prod_img_pic,100) or die('Problem In saving');
        imagedestroy($destimg);
    }
//-------till here------------------     
$pref = '&err=';

if( $success ) {
	$cor = 'Successfully moved uploaded/resized file '; }

// else failed
else {
	$cor = 'Failed to move uploaded file ';
	} 
// -------------------
// updating date txt file
// -------------------

$filename = "check.txt";     // text file name (CHMOD 777);
// Opens up the file declared above for reading and emptying
chmod ($filename, 0777);  
$fp 		= fopen( $filename,"w+");
ftruncate($fp, '0');
	fclose( $fp ); 

$New = "$pref$cor$uploadfile";
$fp = fopen($filename, "w+");
fwrite($fp, $New, 40000);
fclose($fp);


?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.