2

i am trying to resize my image to a classic avatar size, i've trying few thing but i cant make it work :s

It would be great if anyone could help :D Here is the code i am using :

(upload is working)

    <?php
if(isset($_FILES['userfile']['name'])){
    $uploaddir = 'uploads/avatars/';
    $uploadedFileName = $_SESSION['userid'];
    $uploadfile = $uploaddir . basename($_FILES['userfile']['name'], $uploadedFileName);
    $ext = pathinfo($uploadfile, PATHINFO_EXTENSION);

    echo "<p>";

    if (move_uploaded_file($_FILES['userfile']['tmp_name'], "uploads/avatars/".$uploadedFileName.'.'.$ext)) {
      echo "File has been successfully uploaded.\n";
    } else {
       echo "Upload failed";
    }

    echo "</p>";  
} else {
    echo '
    <html>
        <head>
            <meta charset="utf-8" />
            <title>PMM | File Upload</title>
        </head>
        <body>
            <form enctype="multipart/form-data" action="#" method="POST"> 
            <input type="hidden" name="MAX_FILE_SIZE" value="10000000" />
            <input name="userfile" type="file" />
            <input type="submit" value="Send File" />
            </form>
        </body>
    </html>';
} ?> 
CurtN
  • 21
  • 2
  • 1
    Can we see your attempt at image resizing? I imagine you mean "resize after upload", since the server cannot resize before the upload happens. – halfer Apr 17 '15 at 09:26
  • You cannot resize an image `before` it's uploaded, only `after`. – Cristik Apr 17 '15 at 09:29
  • @halfer I ve tried to resize with : imagecopyresampled(); but i didnt manage to make it work. – CurtN Apr 17 '15 at 09:37
  • @Cristik: so maybe should i use something like this : $ResizedImage = functionToResize($myOldImage); – CurtN Apr 17 '15 at 09:37
  • If you wouldn't mind trying something (there are many examples on the web) and then editing that attempt into the question, it'll give us something to advise you on. As it stands it is hard to work out where you are stuck. – halfer Apr 17 '15 at 09:59
  • Here is the class I use to manipulate the image while uploading: [Image manipulator](https://gist.github.com/philBrown/880506) – Cliff Burton Apr 17 '15 at 10:01
  • @Cristik The uploaded image is saved in a *temp* file which is deleted after script ends, you can resize the image giving it a new **height** and **width** before to storing the *temp* file to the final folder – Cliff Burton Apr 17 '15 at 10:07
  • @CliffBurton : yes this is exactly what i want to do :) – CurtN Apr 17 '15 at 10:17
  • So check [this comment](http://stackoverflow.com/questions/29695266/php-how-to-resize-an-image-before-uploading-simple-best-way-to-do-it#comment47528242_29695266) ;) – Cliff Burton Apr 17 '15 at 10:21
  • Thanks @CliffBurton, i will try with that :) – CurtN Apr 17 '15 at 14:39
  • You can not resize before uploading, See this entry for how – Rohit Gupta Apr 18 '15 at 03:03

0 Answers0