/    Sign up×
Community /Pin to ProfileBookmark

image resize save

Below is the start of the code that is supposed to resize images then save the resized image to a folder. I stuck an cannot figure out how to get file name and save the image after it has been resized. I used the same image code but this code saved the file and got the file name. the image was not altered as I’m trying to do.

any help is great

[code=php]
$res = copy($HTTP_POST_FILES[‘file’][‘tmp_name’][$i], $path.$user_name.”-“.$HTTP_POST_FILES[‘file’][‘name’][$i]);

if (!$res) {

$msg .= “upload failed!”;

header(“location: $url?msg=$msg”);

exit;

}

} else {

header(“location: $url?msg=$msg”);

$msg .=”Wrong file type, Must be jpeg, gif or png file types”; exit; }

$image = $user_name.”-“.$HTTP_POST_FILES[‘file’][‘name’][$i];

//Get Image Size
$image_size = $_FILES[‘file’][‘size’][$i];
[/code]

[code=php]
// Upload Images
$path = ‘../user_images/’;
foreach ($_FILES[‘uploadImg’][‘tmp_name’] as $i => $val) :
if (is_uploaded_file($_FILES[‘uploadImg’][‘tmp_name’][$i])) {

/////////////////Check File Extentions

if (($_FILES[‘file’][‘type’][$i]==”image/gif”) || ($_FILES[‘file’][‘type’][$i]==”image/pjpeg”) || ($_FILES[‘file’][‘type’][$i]==”image/jpeg”) || ($_FILES[‘file’][‘type’][$i]==”image/png”)) {

if (file_exists($path . $_FILES[‘file’][‘name’][$i])) {

$msg .= “The file already exists, change your file name”;

header(“location: $url?msg=$msg”);
exit;

}
///////////////////
//// Resize Image Code
// Load image
$image = $_FILES[‘uploadImg’][‘tmp_name’][i];
if ($image === false) { die (‘Unable to open image’); }

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

// New width and height
$new_width = 90;
$new_height = 125;

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

// Display resized image
header(‘Content-type: image/jpeg’);
imagejpeg($image_resized);
die();

///////

endforeach;

}
?>
[/code]

to post a comment
PHP

10 Comments(s)

Copy linkTweet thisAlerts:
@kprocauthorJul 16.2007 — I think that I'm getting closer. I found the code within this script on the php website but I'm getting this message

Warning: Division by zero in /mnt/w0400/d11/s01/b02a5c57/www/familyclick.ca/photos/album.php on line 116

Warning: imagecreatetruecolor() [function.imagecreatetruecolor]: Invalid image dimensions in /mnt/w0400/d11/s01/b02a5c57/www/familyclick.ca/photos/album.php on line 125

Warning: imagecopyresampled(): supplied argument is not a valid Image resource in /mnt/w0400/d11/s01/b02a5c57/www/familyclick.ca/photos/album.php on line 127

any help is excellent

[code=php]
//Upload Images
$path = '../user_images/';
if(isset($_POST['upLoadImages'])){

foreach ($_FILES['file']['tmp_name'] as $i => $val) :
if (is_uploaded_file($_FILES['file']['tmp_name'][$i])) {

/*if ($_FILES['file']['size'][$i]>$max_file_size) {

$msg = "Image Exceeds maximum file size";

header("location: $url?msg=$msg");
exit;

}*/

if (($_FILES['file']['type'][$i]=="image/gif") || ($_FILES['file']['type'][$i]=="image/pjpeg") || ($_FILES['file']['type'][$i]=="image/jpeg") || ($_FILES['file']['type'][$i]=="image/png")) {

if (file_exists($path . $_FILES['file']['name'][$i])) {


$msg .= "The file already exists, change your file name";

header("location: $url?msg=$msg");
exit;


}// Load image

// The file
$filename = $HTTP_POST_FILES['file']['temp'][$i];

// Set a maximum height and width
$width = 200;
$height = 200;

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

// Get new dimensions
list($width_orig, $height_orig) = getimagesize($filename);

$ratio_orig = $width_orig/$height_orig;

if ($width/$height > $ratio_orig) {
$width = $height*$ratio_orig;
} else {
$height = $width/$ratio_orig;
}

// Resample
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);

// Output
echo $image_p;
echo $image;


$res = copy($HTTP_POST_FILES['file']['tmp_name'][$i], $path.$user_name."-".$HTTP_POST_FILES['file']['name'][$i]);

if (!$res) {

$msg .= "upload failed!";

header("location: $url?msg=$msg");

exit;

}

} else {

header("location: $url?msg=$msg");



$msg .="Wrong file type, Must be jpeg, gif or png file types"; exit; }


$image = $user_name."-".$HTTP_POST_FILES['file']['name'][$i];

//Set Image Size
$image_size = $_FILES['file']['size'][$i];
[/code]
Copy linkTweet thisAlerts:
@hastxJul 16.2007 — 

[code=php]


if (file_exists($path . $_FILES['file']['name'][$i])) {
$msg .= "The file already exists, change your file name";
header("location: $url?msg=$msg");
exit;
}
///////////////////
//// Resize Image Code
// Load image
$image = $_FILES['uploadImg']['tmp_name'][i];
if ($image === false) { die ('Unable to open image'); }

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

// New width and height
$new_width = 90;
$new_height = 125;

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

// Display resized image
header('Content-type: image/jpeg');
imagejpeg($image_resized);
die();

///////

endforeach;

}
?>
[/code]
[/QUOTE]


Are you only trying to display the resized image...or are you trying to save the resized image to a directory?
Copy linkTweet thisAlerts:
@kprocauthorJul 16.2007 — I'm trying to upload the resizd image into a folder called "user_images". the details about the image along with its name will be saved in a mysql database. I'm trying to create an online photo album

thanks for the help
Copy linkTweet thisAlerts:
@kprocauthorJul 16.2007 — complete code

this code gives this error message


Warning: imagecopyresampled(): supplied argument is not a valid Image resource in /mnt/w0400/d11/s01/b02a5c57/www/familyclick.ca/photos/album.php on line 119

Resource id #4

thank you for the help

[code=php]
$path = '../user_images/';
if(isset($_POST['upLoadImages'])){

foreach ($_FILES['file']['tmp_name'] as $i => $val) :
if (is_uploaded_file($_FILES['file']['tmp_name'][$i])) {

/*if ($_FILES['file']['size'][$i]>$max_file_size) {

$msg = "Image Exceeds maximum file size";

header("location: $url?msg=$msg");
exit;

}*/

if (($_FILES['file']['type'][$i]=="image/gif") || ($_FILES['file']['type'][$i]=="image/pjpeg") || ($_FILES['file']['type'][$i]=="image/jpeg") || ($_FILES['file']['type'][$i]=="image/png")) {

if (file_exists($path . $_FILES['file']['name'][$i])) {


$msg .= "The file already exists, change your file name";

header("location: $url?msg=$msg");
exit;


}// Load image

// The file
$filename = $HTTP_POST_FILES['file']['temp_name'][$i];

// Set a maximum height and width
$width = 200;
$height = 200;

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

// Get new dimensions
list($width_orig, $height_orig) = getimagesize($filename);

// Resample
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);

// Output
echo $image_p;
echo $image;


$res = copy($HTTP_POST_FILES['file']['tmp_name'][$i], $path.$user_name."-".$HTTP_POST_FILES['file']['name'][$i]);

if (!$res) {

$msg .= "upload failed!";

header("location: $url?msg=$msg");

exit;

}

} else {

header("location: $url?msg=$msg");



$msg .="Wrong file type, Must be jpeg, gif or png file types"; exit; }


$image = $user_name."-".$HTTP_POST_FILES['file']['name'][$i];

//Set Image Size
$image_size = $_FILES['file']['size'][$i];

/*mysql_query("INSERT INTO image_files (id, image_name, image_size, image_for, user_id, create_date)VALUES('$ad_id','$image', '$image_size', 'ads', '$member_id', now())")or die(mysql_error());*/

}
//////////////////////
endforeach;
}
?>
[/code]
Copy linkTweet thisAlerts:
@hastxJul 16.2007 — The script I posted under your last post will do that. This script has several problems with it.
[LIST]
  • [*]This script will display and exit.

  • [*]Also, in trying to constrain both the width and hight to predefined dimensions (while "resizing" the image)...you will get unexpected results.

  • [/LIST]
    Copy linkTweet thisAlerts:
    @kprocauthorJul 16.2007 — I dont want to see the image I just want to send it to the folder and add the information to the mysql table. I'm not sure how to edit the code you posted to do this. The last post I did with all the code, I believe shows the flow of the code
    Copy linkTweet thisAlerts:
    @kprocauthorJul 16.2007 — I found the below code which would do exactly what I want if I can get it to work. I'm getting error messages

    Warning: imagesx(): supplied argument is not a valid Image resource in /mnt/w0400/d11/s01/b02a5c57/www/familyclick.ca/photos/album.php on line 87

    Warning: imagesy(): supplied argument is not a valid Image resource in /mnt/w0400/d11/s01/b02a5c57/www/familyclick.ca/photos/album.php on line 88

    Warning: Division by zero in /mnt/w0400/d11/s01/b02a5c57/www/familyclick.ca/photos/album.php on line 96

    Warning: imagecreatetruecolor() [function.imagecreatetruecolor]: Invalid image dimensions in /mnt/w0400/d11/s01/b02a5c57/www/familyclick.ca/photos/album.php on line 97

    Warning: imagecopyresampled(): supplied argument is not a valid Image resource in /mnt/w0400/d11/s01/b02a5c57/www/familyclick.ca/photos/album.php on line 101

    Warning: imagejpeg(): supplied argument is not a valid Image resource in /mnt/w0400/d11/s01/b02a5c57/www/familyclick.ca/photos/album.php on line 106

    Warning: imagedestroy(): supplied argument is not a valid Image resource in /mnt/w0400/d11/s01/b02a5c57/www/familyclick.ca/photos/album.php on line 108

    Warning: imagedestroy(): supplied argument is not a valid Image resource in /mnt/w0400/d11/s01/b02a5c57/www/familyclick.ca/photos/album.php on line 109

    Warning: imagesx(): supplied argument is not a valid Image resource in /mnt/w0400/d11/s01/b02a5c57/www/familyclick.ca/photos/album.php on line 87

    Warning: imagesy(): supplied argument is not a valid Image resource in /mnt/w0400/d11/s01/b02a5c57/www/familyclick.ca/photos/album.php on line 88

    [code=php]
    //Upload Images
    $path = '../user_images/';
    if(isset($_POST['upLoadImages'])){
    foreach ($_FILES['file']['tmp_name'] as $i => $val) :
    // This is the temporary file created by PHP
    $uploadedfile = $_FILES['file']['tmp_name'][i];

    // Create an Image from it so we can do the resize
    $src = imagecreatefromjpeg($uploadedfile);

    // Capture the original size of the uploaded image
    //list($width,$height)= getimagesize($uploadedfile);

    $width = imagesx($uploadedfile);
    $height = imagesy($uploadedfile);

    // For our purposes, I have resized the image to be
    // 600 pixels wide, and maintain the original aspect
    // ratio. This prevents the image from being "stretched"
    // or "squashed". If you prefer some max width other than
    // 600, simply change the $newwidth variable
    $newwidth=600;
    $newheight=($height/$width)*600;
    $tmp=imagecreatetruecolor($newwidth,$newheight);

    // this line actually does the image resizing, copying from the original
    // image into the $tmp image
    imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);

    // now write the resized image to disk. I have assumed that you want the
    // resized, uploaded image file to reside in the ./images subdirectory.
    $filename = "../user_images/". $_FILES['file']['name'][i];
    imagejpeg($tmp,$filename,100);

    imagedestroy($src);
    imagedestroy($tmp); // NOTE: PHP will clean up the temp file it created when the request
    // has completed.
    endforeach;

    }
    [/code]
    Copy linkTweet thisAlerts:
    @kprocauthorJul 16.2007 — here is the complete script with the form and the current error messages. I changes the code based on infor I found at this website

    http://4wordsystems.com/php_image_resize.php

    any ideas whats wrong

    thank you

    Warning: Division by zero in /mnt/w0400/d11/s01/b02a5c57/www/familyclick.ca/photos/album.php on line 92

    Warning: imagecreatetruecolor() [function.imagecreatetruecolor]: Invalid image dimensions in /mnt/w0400/d11/s01/b02a5c57/www/familyclick.ca/photos/album.php on line 93

    Warning: imagecopyresampled(): supplied argument is not a valid Image resource in /mnt/w0400/d11/s01/b02a5c57/www/familyclick.ca/photos/album.php on line 97

    Warning: imagejpeg(): supplied argument is not a valid Image resource in /mnt/w0400/d11/s01/b02a5c57/www/familyclick.ca/photos/album.php on line 102

    Warning: imagedestroy(): supplied argument is not a valid Image resource in /mnt/w0400/d11/s01/b02a5c57/www/familyclick.ca/photos/album.php on line 104

    Warning: imagedestroy(): supplied argument is not a valid Image resource in /mnt/w0400/d11/s01/b02a5c57/www/familyclick.ca/photos/album.php on line 105


    [code=php]
    // upload Images
    if(isset($_POST['upLoadImages'])){
    foreach ($_FILES['file']['tmp_name'] as $i => $val) :
    // This is the temporary file created by PHP
    $uploadedfile = $_FILES['file']['tmp_name'][i];

    // Create an Image from it so we can do the resize
    $src = imagecreatefromjpeg($uploadedfile);

    // Capture the original size of the uploaded image
    list($width,$height)=getimagesize($uploadedfile);

    // For our purposes, I have resized the image to be
    // 600 pixels wide, and maintain the original aspect
    // ratio. This prevents the image from being "stretched"
    // or "squashed". If you prefer some max width other than
    // 600, simply change the $newwidth variable
    $newwidth=600;
    $newheight=($height/$width)*600;
    $tmp=imagecreatetruecolor($newwidth,$newheight);

    // this line actually does the image resizing, copying from the original
    // image into the $tmp image
    imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);

    // now write the resized image to disk. I have assumed that you want the
    // resized, uploaded image file to reside in the ./images subdirectory.
    $filename = "../users/images/". $_FILES['file']['name'][i];
    imagejpeg($tmp,$filename,100);

    imagedestroy($src);
    imagedestroy($tmp); // NOTE: PHP will clean up the temp file it created when the request
    // has completed.
    endforeach;
    }

    [/code]


    <i>
    </i>&lt;form name="addImages" method="post" enctype="multipart/form-data" action="album.php?album=&lt;?php echo $_GET['album']; ?&gt;"&gt;
    &lt;table class="columntable"&gt;
    &lt;tr&gt;
    &lt;td&gt;&lt;input name="file[]" type="file" id="file[]" /&gt;&lt;/td&gt;
    &lt;td&gt;&lt;input name="file[]" type="file" id="file[]" /&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
    &lt;td&gt;&lt;input name="file[]" type="file" id="file[]" /&gt;&lt;/td&gt;
    &lt;td&gt;&lt;input name="file[]" type="file" id="file[]" /&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
    &lt;td&gt;&lt;input name="file[]" type="file" id="file[]" /&gt;&lt;/td&gt;
    &lt;td&gt;&lt;input name="file[]" type="file" id="file[]" /&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
    &lt;td&gt;&lt;input name="file[]" type="file" id="file[]" /&gt;&lt;/td&gt;
    &lt;td&gt;&lt;input name="file[]" type="file" id="file[]" /&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
    &lt;td colspan="2"&gt;
    &lt;input type="submit" name="upLoadImages" value="Add Photos" /&gt; <br/>
    &lt;input name="clear" type="reset" id="clear" value="Reset" /&gt; &lt;/td&gt;
    &lt;/tr&gt;
    &lt;/table&gt;
    &lt;/form&gt;
    Copy linkTweet thisAlerts:
    @kprocauthorJul 16.2007 — I'm down to this error message

    Warning: imagejpeg() [function.imagejpeg]: Unable to open '../user_images/' for writing: Is a directory in /mnt/w0400/d11/s01/b02a5c57/www/familyclick.ca/photos/album.php on line 103

    any thoughts
    Copy linkTweet thisAlerts:
    @hastxJul 16.2007 — In your script you have "../users/images" ...for filename variable, but it is looking for "../user_images"
    ×

    Success!

    Help @kproc spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.9,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...