Jump to content

[SOLVED] GD help....


djpic

Recommended Posts

I am trying take a photo that is from a user (uploaded via a form) compress is (resize it) and then save it to a directory.  I have the part in which I can copy the file to a directory on the server, I just need a simple code to resize the image to a size I specify.  For example:

 

$image = $_FILE['userfile']['tmp_name'];

 

$image = (function it resize the image)

 

I have done a search on code to do this, but all the ones I find is just too complicated for what I am trying to do.  I know I need to use GD to do it.  Thanks for the help.

Link to comment
Share on other sites

I know how to do the scaling algorithm.  That is not a big deal.  That is the problem, all the samples of code I have seen have this crazy scaling algorithm.  I all I want it just the basic lines of code or commands that allow me to pull the file from the user, scale it (don't worry about the algorithm), and then save the file to a folder on the server.

Link to comment
Share on other sites

Ok here is what I got.  Now I just need to know a few things.

 

First: it saved the file in the test directory but does not save the compressed file in the directory, just copies the original one.  How can I get it to save the compressed or resized image in the test directory?  I tried just changing the:

move_uploaded_file($_FILES['userfile']['tmp_name'], $Destination)

To:

move_uploaded_file($image_resized, $Destination)

But didn't work.

 

Second: When it does save the file, the permissions are set to 600 instead of 777.  How can I fix this?

 

Here is the code:

<?php
  // Load image
  $UserImage = $_FILES['userfile']['tmp_name'];

  $image = @imagecreatefromjpeg($UserImage);

  // Get original width and height
  $width = imagesx($image);
  $height = imagesy($image);

  // New width and height
  $new_width = 150;
  $new_height = 100;

  // Resample
  $image_resized = imagecreatetruecolor($new_width, $new_height);
  imagecopyresampled($image_resized, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

  $Directory = 'test/';
  $Destination = $Directory . basename($_FILES['userfile']['name']);

  // Upload Image
  echo '<pre>';
  if (move_uploaded_file($_FILES['userfile']['tmp_name'], $Destination)) {
    echo "File is valid, and was successfully uploaded.\n";
  } else {
    echo "Possible file upload attack!\n";
  }

  echo 'Here is some more debugging info:';
  print_r($_FILES);

  print "</pre>";
?>

 

Link to comment
Share on other sites

Second: When it does save the file, the permissions are set to 600 instead of 777.  How can I fix this?

 

Try using chmod()

 

For the scaling I would set either the height or the width to a static value. With the static height/width values you used, you will skew the image. Try something like


  // Get original width and height
  $width = imagesx($image);
  $height = imagesy($image);

  // New width and height
  $new_width = 150;
  $new_height = $height * ($new_width / $width);

Now it should be proportionate. As for saving the newly created file, I've never had to do that. I have always just created the image on the fly and sent it to the browser.

 

Maybe create a new file with fopen then use fwrite to write the output from imagejpeg into the file. Someone else might have a better idea.

Link to comment
Share on other sites

I have tried the chmod() command but doesn't seem to do anything.  I believe I am doing it right:

chmod("file.jpg",777);

 

I know how to keep the files proportionate thanks though.  I am not really worried about that right now, just want to get it to work the way I want.  I guess I could have it compress the files every time they are viewed but I am sure that will be a strain on the server.

Link to comment
Share on other sites

Ok, thanks Barand...that did what I wanted to do and fixed the 600 permission problem.  Single line of code, perfect!  Didn't know that function could do that.  Anyways, here is the final code:

<?
  // Load image
  $UserImage = $_FILES['userfile']['tmp_name'];

  $image = @imagecreatefromjpeg($UserImage);

  // Get original width and height
  $width = imagesx($image);
  $height = imagesy($image);

  // New width and height
  $new_width = 150;
  $new_height = 100;

  // Resample
  $image_resized = imagecreatetruecolor($new_width, $new_height);
  imagecopyresampled($image_resized, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
  
  $Destination = "test/test.jpg";
  
  imagejpeg($image_resized, $Destination, 100);
?>

 

Link to comment
Share on other sites

That scaling algorithm is as simple as it gets, but if the user image is in portrait format instead of landscape then it going to get distorted.

 

I prefer to set a size of the square that the thumbnail should fit into (say 150x150) then scale depending on which is larger, original height or original width

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.