Jump to content

PHP Image upload ans resize


drisate

Recommended Posts

I need to upload 3 images and resize them as part of a forme but for some reason, the images does not end up in the server directory. What am i missing?

 

The fille array looks like this:

Array
(
    [file1] => Array
        (
            [name] => s_casafolio.png
            [type] => image/png
            [tmp_name] => /tmp/phpENy7j4
            [error] => 0
            [size] => 2047496
        )

    [file2] => Array
        (
            [name] => lawjournal.png
            [type] => image/png
            [tmp_name] => /tmp/phpT0fu1Y
            [error] => 0
            [size] => 1139448
        )

    [file3] => Array
        (
            [name] => s_mdj_2.png
            [type] => image/png
            [tmp_name] => /tmp/phpUsRk6W
            [error] => 0
            [size] => 792873
        )

)

And my processing looks like this:

<?php

$tmp = md5(md5(date("YmdHis")));
$img_quality = 70;
$maxDim = 700;
$dest_folder = "/home/user/public_html/userfiles/inscription_designer/";
$valid_mime_types_images = array(
    "image/gif",
    "image/png",
    "image/jpeg",
    "image/pjpeg",
);

if (in_array($_FILES["file1"]["type"], $valid_mime_types_images)) {
    
    $ext = pathinfo($_FILES['file1']['name'], PATHINFO_EXTENSION);
    $dest_file = $dest_folder.$tmp.'1.'.$ext;
    $fn = $_FILES['file1']['tmp_name'];
    $size = getimagesize($fn);
    $ratio = $size[0]/$size[1]; // width/height
    if( $ratio > 1) {
        $width = $maxDim;
        $height = $maxDim/$ratio;
    }
    else {
        $width = $maxDim*$ratio;
        $height = $maxDim;
    }
    $src = imagecreatefromstring(file_get_contents($fn));
    $dst = imagecreatetruecolor($width,$height);
    imagecopyresampled($dst,$src,0,0,0,0,$width,$height,$size[0],$size[1]);
    imagedestroy($src);
    imagepng($dst,$dest_file); // adjust format as needed
    imagedestroy($dst);
    
}else{
    $err .= "- Image 1 est invalide.<br />";
}

if (in_array($_FILES["file2"]["type"], $valid_mime_types_images)) {
    
    $ext = pathinfo($_FILES['file2']['name'], PATHINFO_EXTENSION);
    $dest_file = $dest_folder.$tmp.'2.'.$ext;
    $fn = $_FILES['file2']['tmp_name'];
    $size = getimagesize($fn);
    $ratio = $size[0]/$size[1]; // width/height
    if( $ratio > 1) {
        $width = $maxDim;
        $height = $maxDim/$ratio;
    }
    else {
        $width = $maxDim*$ratio;
        $height = $maxDim;
    }
    $src = imagecreatefromstring(file_get_contents($fn));
    $dst = imagecreatetruecolor($width,$height);
    imagecopyresampled($dst,$src,0,0,0,0,$width,$height,$size[0],$size[1]);
    imagedestroy($src);
    imagepng($dst,$dest_file); // adjust format as needed
    imagedestroy($dst);
    
}else{
    $err .= "- Image 2 est invalide.<br />";
}

if (in_array($_FILES["file3"]["type"], $valid_mime_types_images)) {
    $ext = pathinfo($_FILES['file3']['name'], PATHINFO_EXTENSION);
    $dest_file = $dest_folder.$tmp.'3.'.$ext;
    $fn = $_FILES['file3']['tmp_name'];
    $size = getimagesize($fn);
    $ratio = $size[0]/$size[1]; // width/height
    if( $ratio > 1) {
        $width = $maxDim;
        $height = $maxDim/$ratio;
    }
    else {
        $width = $maxDim*$ratio;
        $height = $maxDim;
    }
    $src = imagecreatefromstring(file_get_contents($fn));
    $dst = imagecreatetruecolor($width,$height);
    imagecopyresampled($dst,$src,0,0,0,0,$width,$height,$size[0],$size[1]);
    imagedestroy($src);
    imagepng($dst,$dest_file); // adjust format as needed
    imagedestroy($dst);
    
}else{
    $err .= "- Image 3 est invalide.<br />";
}
?>
Link to comment
Share on other sites

The problem could be pretty much anything, maybe an application bug, maybe an issue with the PHP settings, maybe incorrect permissions on the server.

 

So you'll have to narrow it down yourself. Check the error log (if it's disabled, turn it on now and try again). What do you see?

 

You also need to learn about using functions. Copy-pasting of large bocks of code is not how programming works and will make debugging much harder.

Edited by Jacques1
Link to comment
Share on other sites

The problem could be pretty much anything, maybe an application bug, maybe an issue with the PHP settings, maybe incorrect permissions on the server.

 

So you'll have to narrow it down yourself. Check the error log (if it's disabled, turn it on now and try again). What do you see?

 

You also need to learn about using functions. Copy-pasting of large bocks of code is not how programming works and will make debugging much harder.

 

Yes, break down the various functions into modules, that makes life way easier.

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.