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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.