Hello I am trying to build a simple image edit script in PHP. What I want this script to do is:
Upload file to temp directory, retrieve the path to the temp file, retrieve variables 'title' and 'subtitle'.
Perform some error checking to make sure nothing funny is going on.
Create a NEW black image which is 1.5 the size of the temporary image.
COPY the temporary image into the center of the new black image, retaining original size
Output this file as a jpeg directly to the browser.
This is the code that I have.
SIMPLE HTML UPLOAD TEST FORM
<html>
<head></head>
<body>
<form method="post" action="gen_funny_pic.php">
<input type="file" name="uploadedpicture"><BR>
<input type="text" name="title"><BR>
<input type="text" name="subtitle"><BR>
<input type="submit" value="submit">
</form>
</body>
</html>
CODE FOR GENERATING IMAGE AND OUTPUTTING AS JPEG
<?php
//Filename: Gen_funny_pic.php
//Description: Lets the user generate a custom funny picture by allowing them to upload thier pic, and
//add a title and subtitle.
//Define some variables
$file_id = $_FILES['uploadedpicture']['tmp_name'];
$file_size = $_FILES['uploadedpicture']['size'] / 1024;
$file_error_code = $_FILES['uploadedpicture']['error'];
$string_title = $_REQUEST['title'];
$string_subtitle = $_REQUEST['subtitle'];
$max_title_size = 20; //In chars
$max_subtitle_size = 20; //In chars
$max_file_size = 1024; //In KB - 1024kb = 1mb
$new_image_scale = 1.5;
//Check for errors.
if ($string_title == "" || $string_subtitle == "")
exit("Error: Title or Subtitle don't exist!");
if ($string_title > $max_title_size)
exit("Error: Title is too long!");
if ($string_subtitle > $max_subtitle_size)
exit("Error: Subtitle is too long!");
if ($file_id == "")
exit("Error: Error uploading file!");
if ($file_size > $max_file_size)
exit("Error: File too large!");
if ($file_error_code > 0)
exit("Error: Error uploading file!");
//Start creating our image.
$old_image = open_image($file_id);
if ($old_image == false)
exit("Error: Error processing file!");
$old_height = imagesy($old_image);
$old_width = imagesx($old_image);
$new_height = $old_height * $new_image_scale;
$new_width = $old_width * $new_image_scale;
$new_image_top_left_x = ($new_width / 2) - ($old_width / 2);
$new_image_top_left_y = ($new_height / 2) - ($old_height / 2);
$new_image = imagecreatetruecolor($new_width, $new_height);
$black = imagecolorallocate($new_image, 0, 0, 0);
imagefilltoborder($new_image, 0, 0, $black, $black);
imagecopyresampled($new_image, $old_image, $new_image_top_left_x, $new_image_top_left_y, 0, 0, $old_width, $old_height, $old_width, $old_height);
//Display image.
header("Content-type: image/jpeg");
imagejpeg($new_image);
imagedestroy($new_image);
?>
At the moment I am receiving the error "Error: Error uploading file!" which means there is a problem with either of these lines:
if ($file_id == "")
if ($file_error_code > 0)
Basically, I don't really know what I am doing, and I am looking for some help (once I can do this I will be able to figure out the other GD functions)
Also could you please suggest improvements as to how to improve the script.
Thankyou. 😉