Jump to content

PT2: PHP, image manipulation, Head, Brick Wall, Bang... Bang, Bang!!!!


Mouse

Recommended Posts

APOLOGIES! this is a biggy!
Well here goes again, I thought I’d got this one squared away but alas no. I found and modified the primary functions on this script which uploads and crops member photos for my site and it does these things well. But when it comes to saving the images in the file on my server it all goes pear shaped. Any help would be most welcome.

Code: Setting Session variables in the main sign up page of my site Form1.php.
[code]
// Set the first name and family name in a session to pick them up after.
$_SESSION['firstname'] = $s_firstname;
$_SESSION['familyname'] = $s_familyname;
[/code]
Code: triggering the JavaScript pop up for the image uploader, including the session variables in the URL.
[code]
<?php
$userNAME = trim($_SESSION['firstname'])."_".trim($_SESSION['familyname']);
$userNAME = strtolower($userNAME);

$cropHREF = "\"javascript:void(window.open('http://mouse.nodstrum.com/img_up/cropper.php?imageNAME=".$userNAME."', 'uploader', 'toolbar=0,scrollbars=0,location=0,statusbar=1,menubar=0,resizable=0,width=640,height=580'));\""
?>

<B TITLE='header=[Photo Upload] body=[Choose a good clear head and shoulders photo of yourself preferably in your Disney days… The photos will be checked so nothing too outlandish please.]'><a href=<?php echo $cropHREF ;?><strong>Click here to upload your photo</strong></a> <font color="#3399FF"<i> (?)</i></b>
<!-- IMG_UP test ends here -->
The modified Cropper.php script.

<?php
// This script stores temporary image data in the user's session
session_start();
// print_r($_GET);
// Names section added by MS 14-Jan-2007
if (isset($_GET['imageNAME'])) {
$_SESSION['imageName'] = $_GET['imageName'];
}
// print_r($_GET);

// end of Names section added by MS 14-Jan-2007

// The dimensions the uploaded image is resized to.
// (The aspect ratio is maintained and no quality is lost during the final crop no matter how
// small you set these values)
$default_max_width = 640;
$default_max_height = 480;

// The size of the image the user will create:
$cropped_width = 140;
$cropped_height = 160;

// A name for the session variable the script will use (must be unique for your domain)
$unique_session_key = 'imgcropper';

// A directory that the webserver can create files in, read from and delete files from
// (the trailing slash is required)
define('TEMP_DIR','temp/');

// Before the user uploads their own image they get to play with this one:
$default_image = 'default.jpg';

// A semi transparent gif used to create the darkened effect around the edge of the page.
$border_background = 'screen.gif';


/* // After the user has chosen the area to crop, the cropped image is passed to the following
// function that you must write your own code for:
function storeImage($img_id)
{
// $img_id is a GD image reference, if you want the actual JPEG data pass it to imgdata (as below)
$_SESSION['imgcropper']['cropped'] = imgdata($img_id);
}
*/
// NEW FROM HERE!
// After the user has chosen the area to crop, the cropped
// image is passed to the following function that you must
// write your own code for:
function storeImage($img_id)
{
// $img_id is a GD image reference, if you want the actual
// JPEG data pass it to imgdata (as below)
$fp = fopen('/images/mugs/'.$_SESSION['imageName'].'.jpg','wb');
fwrite($fp, imgdata($img_id));
fclose($fp);
}

// ...then the user is redirected to this page:
$redirect_after_crop = 'done.php';

// All of the code:
include("cropper-include.php");

?>
[/code]
Link to comment
Share on other sites

PT2: PHP, Head, Brick Wall, Bang... Bang, Bang!!!!

The modified Cropper_include script.
[code]
<?php

// handle requests for temporary low-res image:
if(isset($_GET['showimg']))
{
header("Content-Type: image/jpeg");
echo $_SESSION[$unique_session_key]['lowres'];
die();
}

// handle photo uploads:
$photoerr = false;
if(isset($_FILES['photo']))
{
$img = imagecreatefromjpeg($_FILES['photo']['tmp_name']);
if($img=="")
{
// bad image uploaded
$photoerr = true;
}
else
{
$sizedata = getimagesize($_FILES['photo']['tmp_name']);
// original dimensions:
$orig_w = $sizedata[0];
$orig_h = $sizedata[1];
// low-res image dimensions:
$full_w = min($default_max_width, $orig_w);
$full_h = round(($full_w/$orig_w)*$orig_h);
if($full_h > $default_max_height)
{
$full_h = $default_max_height;
$full_w = round(($full_h/$orig_h)*$orig_w);
}
// create resized image:
$full = imagecreatetruecolor($full_w, $full_h);
imagecopyresized($full, $img, 0, 0, 0, 0, $full_w, $full_h, $orig_w, $orig_h);
// store in Session:
$_SESSION[$unique_session_key]['full'] = imgdata($img);
$_SESSION[$unique_session_key]['lowres'] = imgdata($full);
}
}

// handle crop requests:
if(isset($_POST['top']) && isset($_POST['left']) && isset($_POST['width']) && isset($_POST['height']))
{
do { $tempfile = rand(1,10000); } while(file_exists(TEMP_DIR.$tempfile));
$fp = fopen(TEMP_DIR.$tempfile, 'wb');
fwrite($fp, $_SESSION[$unique_session_key]['full']);
fclose($fp);
$sizedata = getimagesize(TEMP_DIR.$tempfile);
$orig_w = $sizedata[0];
$orig_h = $sizedata[1];
$img = imagecreatefromjpeg(TEMP_DIR.$tempfile);
$full_w = intval($_POST['width']);
$full_h = intval($_POST['height']);
$full = imagecreatetruecolor($full_w, $full_h);
imagecopyresized($full, $img, 0, 0, 0, 0, $full_w, $full_h, $orig_w, $orig_h);
$final = imagecreatetruecolor($cropped_width, $cropped_height);
imagecopy($final, $full, 0, 0, -1*(intval($_POST['left'])-250), -1*(intval($_POST['top'])-160), $cropped_width, $cropped_height);
storeImage($final);
unlink(TEMP_DIR.$tempfile);
header("Location: $redirect_after_crop");
}

if(!isset($_SESSION[$unique_session_key]['full']))
{
$_SESSION[$unique_session_key]['full'] = file_get_contents($default_image);
$_SESSION[$unique_session_key]['lowres'] = file_get_contents($default_image);
}

function imgdata($imgid)
{
do{ $filename = TEMP_DIR.rand(1,10000); } while(file_exists($filename));
imagejpeg($imgid, $filename);
$data = file_get_contents($filename);
unlink($filename);
return $data;
}
?>
CSS Removed
JavaScript Removed
<!--added next line TJ 14/01/07 Grrrr -->
<?php echo "name here " .$_SESSION['imageName'] ;?>

<?php if($photoerr) echo "<b>Error: The photo you attempted to upload was not a valid JPEG file.</b>"; ?>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data">
<br />
<span class="intro">Add your photo to your profile page:</span><br />
<input type="file" name="photo" />
<input type="submit" value="Upload your photo" />
</form>
</div>
<div class="ontop" id="saveform">
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="hidden" id="top" name="top" value="-1" />
<input type="hidden" id="left" name="left" value="-1" />
<input type="hidden" id="width" name="width" value="-1" />
<input type="hidden" id="height" name="height" value="-1" />
<input type="submit" value="Click HERE to crop and save your photo" />
</form>
</div>
<div id="fadebox1" class="fadebox"></div>
<div id="fadebox2" class="fadebox"></div>
<div id="fadebox3" class="fadebox"></div>
<div id="fadebox4" class="fadebox"></div>
<img src="<?php echo $_SERVER['PHP_SELF']; ?>?showimg=1" id="dragimg" />

</body>
</html>
[/code]
The offending page!
http://mouse.nodstrum.com/form3.php
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.