Ignoring my shocking use of the mysql_*
extension (and any associated issues), could you take a look at my script that uploads an image, adds it to a database, creates a thumbnail of the image 578px in width, and adds that to the database too?
<?php
require_once("validate.php");
while ($hq = mysql_fetch_assoc($hash_query)) {
$ukip_name = $hq['ukip_name'];
$speaker_id = $hq['speaker_id'];
$conference_id = $hq['conference_id'];
$conf_title = $hq['conf_title'];
$webmaster_id = $hq['webmaster_id'];
$first_name = $hq['first_name'];
$family_name = $hq['family_name'];
$job_title = $hq['job_title'];
$company_name = $hq['company_name'];
$country = $hq['country'];
$speaker_photo = $hq['speaker_photo']; // Default = NULL
$company_logo = $hq['company_logo']; // Default = NULL
}
$errors = $_FILES['file']['error'];
$tempname = $_FILES['file']['tmp_name'];
$filename = $_FILES['file']['name'];
$filetype = $_FILES['file']['type'];
$filesize = $_FILES['file']['size'];
$extensions = array("jpg", "jpeg", "JPG", "JPEG");
$expl = explode(".", $filename);
$extension = end($expl);
if ((($filetype == "image/jpg") ||
($filetype == "image/jpeg")) && in_array($extension, $extensions)) {
if ($errors == 0) {
if ($filesize <= 10485760) {
$timestamp = date("ymd-His");
$filename = $speaker_id . "_" . $timestamp . "_photo." . $extension;
$db_insert = mysql_query("INSERT INTO temp_uploads
SET speaker_id = $speaker_id, image_filename = '$filename', image_type = 1");
move_uploaded_file($tempname, "uploads/" . $filename);
// Resize JPEG
$md = explode(".", $filename);
if (end($md) != "eps") {
$filedims = getimagesize("uploads/" . $filename);
$md_width = 578;
$md_height = ($filedims[1] / $filedims[0]) * $md_width;
$filename_md = $md[0] . "_md." . end($md);
$quality = 100;
$src = "uploads/" . $filename;
$new = imagecreatefromjpeg($src);
$canvas = imagecreatetruecolor($md_width, $md_height);
imagecopyresampled($canvas, $new, 0, 0, 0, 0, $md_width, $md_height, $filedims[0], $filedims[1]);
imagejpeg($canvas, "uploads/" . $filename_md, $quality);
$db_insert_md = mysql_query("INSERT INTO temp_uploads
SET speaker_id = $speaker_id, image_filename = '$filename_md', image_type = 1");
}
mysql_close();
header("Location: crop.php?hash={$hash}&file={$filename_md}");
exit();
} else {
closeCon(105, "index", "&hash={$hash}");
}
} else {
closeCon(104, "index", "&hash={$hash}");
}
} else {
closeCon(103, "index", "&hash={$hash}");
}
?>
I do checks on the filesize, so that something over 10MB is disallowed, but I have hit out-of-memory PHP errors on line 43 ($new = imagecreatefromjpeg($src);
) a couple of times recently - one on an 8MB file, and one on a 3.5MB file, which is worrying. These aren't dropping me out of the script in the expected way (closeCon(105...)
) but with a PHP fatal error.
I have temporarily set my initiation file to memory_limit=-1
whilst I try and resolve the error, but I was hoping someone here could look at ways of streamlining my code so as to free up as much memory as possible, as well as suggesting general improvements (again, ignoring any mysql_*
related ones - I know, and I would use mysqli_*
if I could).