Jump to content

Uploaded Image, change width and height


nightkarnation

Recommended Posts

Hey guys!

I have the following php code that grabs variables (and the browsed image) from Flash.

 

//FLASH VARIABLES
$Name = $_POST['Name'];
$itemNumber = $_POST['itemNumber'];
$filename = $_FILES['Filedata']['name'];	
$filetmpname = $_FILES['Filedata']['tmp_name'];
$fileType = $_FILES["Filedata"]["type"];
$fileSizeMB = ($_FILES["Filedata"]["size"] / 1024 / 1000);


list($filename, $extension) = explode('.', basename($_FILES['Filedata']['name']));
$filename = $Name;
$target = $filename . $itemNumber . "." . $extension;


// Place file on server, into the images folder
move_uploaded_file($_FILES['Filedata']['tmp_name'], "images/".$target);

 

 

This works perfect, but what I want to change is the width and height of the uploaded image.

Any ideas/suggestions on how this could be done?

 

Thanks in advance!!

Cheers!

Link to comment
Share on other sites

There's a couple ways todo this... I believe the standard would be using the PHP GD library, but I found Imagick to be very very easy to use.

 

Here is an example I used before...

	
       
        //upload image
$target = 'pics/'.$event.'/';
$target = $target . basename( $_FILES['uploaded']['name']) ;
$ok=1;
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)){
   echo "0";
} else {
   echo "1";
}

//create thumbnail
$im = new Imagick('pics/'.$event.'/'.$_FILES['uploaded']['name']); 
$im->thumbnailImage(150,150); 
$im->writeImage($dir.'/thumb_'.$_FILES['uploaded']['name']);

 

Link to comment
Share on other sites

First, check to see if imagick is install on your server with a php info. Once you get it installed its pretty simple.

 

// Place file on server, into the images folder
move_uploaded_file($_FILES['Filedata']['tmp_name'], "images/".$target);

$im = new Imagick('$_FILES['Filedata']['tmp_name']); //open your file
$im->thumbnailImage(150,150); //resize
$im->writeImage($_FILES['Filedata']['tmp_name']); //write the thumbnail to the server

 

 

Link to comment
Share on other sites

The way I did this...

 

$data = $your_result_resource["image_field_from_table"];

$size = 50

$img = imagecreatefromstring($data);

$width = imagesx($img);

$height = imagesy($img);

$aspect_ratio = $height/$width;

 

if ($width <= $size) {

  $new_w = $width;

  $new_h = $height;

} else {

  $new_w = $size;

  $new_h = abs($new_w * $aspect_ratio);

}

 

$new = imagecreatetruecolor($new_w,$new_h);

imagecopyresampled($new,$img,0,0,0,0,$new_w,$new_h,$width,$height);

 

header('Content-type: image/jpeg');

imagejpeg($new);

imagedestroy($new);

 

I hope it will work for you too....

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.