Jump to content

Image Upload and Cropping script


pedromau

Recommended Posts

hello people!

 

I'm using a flash system that allows user to upload images to pimp their news... but there's something wrong with my script...

Here's what's happening (I need to create a thumb exactly 283 x 150 px):

- The original image is uploaded into the originals folder...no problem here;

- If it is an horizontal image, it creates and upload the images, but the dimensions aren't correct... only width match!

- If it is a vertical image, no thumb is created, but original image is uploaded to originals...

 

Here's the script:

<?php
$storage = 'originals';//create this dir and chmod to 777, here the uoloaded files are stored in original dimensions;
                
// full path/filename to save
$uploadfile = "$storage/" . 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 and transferto  dir news_img -----
  
    $sizew = 283;$sizeh = 150;//the picture's maximal width and height   (requested)
  
$picdir = 'news_img/'; //create this directory for the resized image and chmod to 777;    
    
    $maxfile = '50000';//the picture's maximal size in KB
    $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 = $storage.$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]  <= $sizeh) && ($sizes[0] <=$sizew)){ //if both smaller or equal than resp 283(w) 150(h) do NOTHING
                    $new_width = $sizes[0];
				$new_height = $sizes[1];        }
        if (($sizes[0] > $sizew) && ($sizes[1]>$sizeh))        {// if width > 283 and height>150 
		$ratio_0=$sizes[0]/$sizew;//ratio width<>283
		$ratio_1=$sizes[1]/$sizeh;//ratio height<>150
		if($ratio_0>=$ratio_1){
			$new_height = $sizeh;
			$new_width = abs($new_height*$sizes[0]/$sizes[1]);  }
		if($ratio_1>$ratio_0){
			$new_width = $sizew;
			$new_height = abs($new_width*$sizes[1]/$sizes[0]); 
			  }
		}
	if (($sizes[0] > $sizew) && ($sizes[1]<=$sizeh)){ // width is >283 and height<=150
		$aspect_ratio = $sizes[1]/$sizes[0]; 
		$new_width = $sizew;
		$new_height = abs($new_width*$aspect_ratio);
	} 
        if (($sizes[0] <= $sizew) && ($sizes[1]>$sizeh)){ // width is <=283 and height>150
		$aspect_ratio = $sizes[0]/$sizes[1]; 
		$new_height = $sizeh;
		$new_width = 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,90) or die('Problem In saving');
        imagedestroy($destimg);
    }
?>

 

It should work linke this:

1.jpg

 

Can someone help me out?

 

Link to comment
https://forums.phpfreaks.com/topic/81656-image-upload-and-cropping-script/
Share on other sites

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.