Jump to content

rcouser

Members
  • Posts

    60
  • Joined

  • Last visited

Everything posted by rcouser

  1. Hello folks, Would anyone be able to help with adding a watermark image to the following resize script. <?php // Smart Image Resizer 1.4.1 // Resizes images, intelligently sharpens, crops based on width:height ratios, color fills // transparent GIFs and PNGs, and caches variations for optimal performance ///////////////////// // PARAMETERS ///////////////////// // Parameters need to be passed in through the URL's query string: // image absolute path of local image starting with "/" (e.g. /images/toast.jpg) // width maximum width of final image in pixels (e.g. 700) // height maximum height of final image in pixels (e.g. 700) // color (optional) background hex color for filling transparent PNGs (e.g. 900 or 16a942) // cropratio (optional) ratio of width to height to crop final image (e.g. 1:1 or 3:2) // nocache (optional) does not read image from the cache // quality (optional, 0-100, default: 90) quality of output image ///////////////////// // EXAMPLES ///////////////////// // Resizing a JPEG: // <img src="/image.php/image-name.jpg?width=100&height=100&image=/path/to/image.jpg" alt="Don't forget your alt text" /> // Resizing and cropping a JPEG into a square: // <img src="/image.php/image-name.jpg?width=100&height=100&cropratio=1:1&image=/path/to/image.jpg" alt="Don't forget your alt text" /> // Matting a PNG with #990000: // <img src="/image.php/image-name.png?color=900&image=/path/to/image.png" alt="Don't forget your alt text" /> ///////////////////// // CODE STARTS HERE ///////////////////// if (!isset($_GET['image'])){ header('HTTP/1.1 400 Bad Request'); echo 'Error: no image was specified'; exit(); } define('MEMORY_TO_ALLOCATE', '100M'); define('DEFAULT_QUALITY', 100); define('DOCUMENT_ROOT', $_SERVER['DOCUMENT_ROOT']); define('CURRENT_DIR', dirname(__FILE__)); define('CACHE_DIR_NAME', '/assets/images/image_cache/'); define('CACHE_DIR', DOCUMENT_ROOT . CACHE_DIR_NAME); // Images must be local files, so for convenience we strip the domain if it's there $image = preg_replace('/^(s?f|ht)tps?:\/\/[^\/]+/i', '', (string) $_GET['image']); // For security, directories cannot contain ':', images cannot contain '..' or '<', and // images must start with '/' if ($image{0} != '/' || strpos(dirname($image), ':') || preg_match('/(\.\.|<|>)/', $image)){ header('HTTP/1.1 400 Bad Request'); echo 'Error: malformed image path. Image paths must begin with \'/\''; exit(); } // If the image doesn't exist, or we haven't been told what it is, there's nothing // that we can do if (!$image){ header('HTTP/1.1 400 Bad Request'); echo 'Error: no image was specified'; exit(); } // Strip the possible trailing slash off the document root $docRoot = preg_replace('/\/$/', '', DOCUMENT_ROOT); if (!file_exists($docRoot . $image)){ header('HTTP/1.1 404 Not Found'); echo 'Error: image does not exist: ' . $docRoot . $image; exit(); } // Get the size and MIME type of the requested image $size = GetImageSize($docRoot . $image); $mime = $size['mime']; // Make sure that the requested file is actually an image if (substr($mime, 0, 6) != 'image/'){ header('HTTP/1.1 400 Bad Request'); echo 'Error: requested file is not an accepted type: ' . $docRoot . $image; exit(); } $width = $size[0]; $height = $size[1]; $maxWidth = (isset($_GET['width'])) ? (int) $_GET['width'] : 0; $maxHeight = (isset($_GET['height'])) ? (int) $_GET['height'] : 0; if (isset($_GET['color'])){ $color = preg_replace('/[^0-9a-fA-F]/', '', (string) $_GET['color']); }else{ $color = FALSE; } // If either a max width or max height are not specified, we default to something // large so the unspecified dimension isn't a constraint on our resized image. // If neither are specified but the color is, we aren't going to be resizing at // all, just coloring. if (!$maxWidth && $maxHeight){ $maxWidth = 99999999999999; }elseif ($maxWidth && !$maxHeight){ $maxHeight = 99999999999999; }elseif ($color && !$maxWidth && !$maxHeight){ $maxWidth = $width; $maxHeight = $height; } // If we don't have a max width or max height, OR the image is smaller than both // we do not want to resize it, so we simply output the original image and exit if ((!$maxWidth && !$maxHeight) || (!$color && $maxWidth >= $width && $maxHeight >= $height)){ $data = file_get_contents($docRoot . '/' . $image); $lastModifiedString = gmdate('D, d M Y H:i:s', filemtime($docRoot . '/' . $image)) . ' GMT'; $etag = md5($data); doConditionalGet($etag, $lastModifiedString); header("Content-type: $mime"); header('Content-Length: ' . strlen($data)); echo $data; exit(); } // Ratio cropping $offsetX = 0; $offsetY = 0; if (isset($_GET['cropratio'])){ $cropRatio = explode(':', (string) $_GET['cropratio']); if (count($cropRatio) == 2) { $ratioComputed = $width / $height; $cropRatioComputed = (float) $cropRatio[0] / (float) $cropRatio[1]; if ($ratioComputed < $cropRatioComputed) { // Image is too tall so we will crop the top and bottom $origHeight = $height; $height = $width / $cropRatioComputed; $offsetY = ($origHeight - $height) / 2; } else if ($ratioComputed > $cropRatioComputed) { // Image is too wide so we will crop off the left and right sides $origWidth = $width; $width = $height * $cropRatioComputed; $offsetX = ($origWidth - $width) / 2; } } } // Setting up the ratios needed for resizing. We will compare these below to determine how to // resize the image (based on height or based on width) $xRatio = $maxWidth / $width; $yRatio = $maxHeight / $height; if ($xRatio * $height < $maxHeight) { // Resize the image based on width $tnHeight = ceil($xRatio * $height); $tnWidth = $maxWidth; } else // Resize the image based on height { $tnWidth = ceil($yRatio * $width); $tnHeight = $maxHeight; } // Determine the quality of the output image $quality = (isset($_GET['quality'])) ? (int) $_GET['quality'] : DEFAULT_QUALITY; // Before we actually do any crazy resizing of the image, we want to make sure that we // haven't already done this one at these dimensions. To the cache! // Note, cache must be world-readable // We store our cached image filenames as a hash of the dimensions and the original filename $resizedImageSource = $tnWidth . 'x' . $tnHeight . 'x' . $quality; if ($color) $resizedImageSource .= 'x' . $color; if (isset($_GET['cropratio'])) $resizedImageSource .= 'x' . (string) $_GET['cropratio']; $resizedImageSource .= '-' . $image; $resizedImage = md5($resizedImageSource); $resized = CACHE_DIR . $resizedImage; // Check the modified times of the cached file and the original file. // If the original file is older than the cached file, then we simply serve up the cached file if (!isset($_GET['nocache']) && file_exists($resized)){ $imageModified = filemtime($docRoot . $image); $thumbModified = filemtime($resized); if($imageModified < $thumbModified) { $data = file_get_contents($resized); $lastModifiedString = gmdate('D, d M Y H:i:s', $thumbModified) . ' GMT'; $etag = md5($data); doConditionalGet($etag, $lastModifiedString); header("Content-type: $mime"); header('Content-Length: ' . strlen($data)); echo $data; exit(); } } // We don't want to run out of memory ini_set('memory_limit', MEMORY_TO_ALLOCATE); // Set up a blank canvas for our resized image (destination) $dst = imagecreatetruecolor($tnWidth, $tnHeight); // Set up the appropriate image handling functions based on the original image's mime type switch ($size['mime']){ case 'image/gif': // We will be converting GIFs to PNGs to avoid transparency issues when resizing GIFs // This is maybe not the ideal solution, but IE6 can suck it $creationFunction = 'ImageCreateFromGif'; $outputFunction = 'ImagePng'; $mime = 'image/png'; // We need to convert GIFs to PNGs $doSharpen = FALSE; $quality = round(10 - ($quality / 10)); // We are converting the GIF to a PNG and PNG needs a compression level of 0 (no compression) through 9 break; case 'image/x-png': case 'image/png': $creationFunction = 'ImageCreateFromPng'; $outputFunction = 'ImagePng'; $doSharpen = FALSE; $quality = round(10 - ($quality / 10)); // PNG needs a compression level of 0 (no compression) through 9 break; default: $creationFunction = 'ImageCreateFromJpeg'; $outputFunction = 'ImageJpeg'; $doSharpen = TRUE; break; } // Read in the original image $src = $creationFunction($docRoot . $image); if (in_array($size['mime'], array('image/gif', 'image/png'))){ if (!$color){ // If this is a GIF or a PNG, we need to set up transparency imagealphablending($dst, false); imagesavealpha($dst, true); } else{ // Fill the background with the specified color for matting purposes if ($color[0] == '#') $color = substr($color, 1); $background = FALSE; if (strlen($color) == 6) $background = imagecolorallocate($dst, hexdec($color[0].$color[1]), hexdec($color[2].$color[3]), hexdec($color[4].$color[5])); else if (strlen($color) == 3) $background = imagecolorallocate($dst, hexdec($color[0].$color[0]), hexdec($color[1].$color[1]), hexdec($color[2].$color[2])); if ($background) imagefill($dst, 0, 0, $background); } } // Resample the original image into the resized canvas we set up earlier ImageCopyResampled($dst, $src, 0, 0, $offsetX, $offsetY, $tnWidth, $tnHeight, $width, $height); if ($doSharpen){ // Sharpen the image based on two things: // (1) the difference between the original size and the final size // (2) the final size $sharpness = findSharp($width, $tnWidth); $sharpenMatrix = array( array(-1, -2, -1), array(-2, $sharpness + 12, -2), array(-1, -2, -1) ); $divisor = $sharpness; $offset = 0; imageconvolution($dst, $sharpenMatrix, $divisor, $offset); } // Make sure the cache exists. If it doesn't, then create it if (!file_exists(CACHE_DIR)){ mkdir(CACHE_DIR, 0755); } // Make sure we can read and write the cache directory if (!is_readable(CACHE_DIR)){ header('HTTP/1.1 500 Internal Server Error'); echo 'Error: the cache directory is not readable'; exit(); } else if (!is_writable(CACHE_DIR)){ header('HTTP/1.1 500 Internal Server Error'); echo 'Error: the cache directory is not writable'; exit(); } // Write the resized image to the cache $outputFunction($dst, $resized, $quality); // Put the data of the resized image into a variable ob_start(); $outputFunction($dst, null, $quality); $data = ob_get_contents(); ob_end_clean(); // Clean up the memory ImageDestroy($src); ImageDestroy($dst); // See if the browser already has the image $lastModifiedString = gmdate('D, d M Y H:i:s', filemtime($resized)) . ' GMT'; $etag = md5($data); doConditionalGet($etag, $lastModifiedString); // Send the image to the browser with some delicious headers header("Content-type: $mime"); header('Content-Length: ' . strlen($data)); echo $data; function findSharp($orig, $final){ $final = $final * (750.0 / $orig); $a = 52; $b = -0.27810650887573124; $c = .00047337278106508946; $result = $a + $b * $final + $c * $final * $final; return max(round($result), 0); } // findSharp() function doConditionalGet($etag, $lastModified){ header("Last-Modified: $lastModified"); header("ETag: \"{$etag}\""); $if_none_match = isset($_SERVER['HTTP_IF_NONE_MATCH']) ? stripslashes($_SERVER['HTTP_IF_NONE_MATCH']) : false; $if_modified_since = isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ? stripslashes($_SERVER['HTTP_IF_MODIFIED_SINCE']) : false; if (!$if_modified_since && !$if_none_match) return; if ($if_none_match && $if_none_match != $etag && $if_none_match != '"' . $etag . '"') return; // etag is there but doesn't match if ($if_modified_since && $if_modified_since != $lastModified) return; // if-modified-since is there but doesn't match // Nothing has changed since their last request - serve a 304 and exit header('HTTP/1.1 304 Not Modified'); exit(); } // doConditionalGet() ?> Any help would be much appreciated. Thanks
  2. Brilliant Psycho, thats exactly what I need. Thanks very much everyone for your help.
  3. When overwrite_prevent_ordering equals 1 (CURDATE() < t1.end_date AND t1.prevent_date IS NULL) OR (CURDATE() < t1.end_date AND CURDATE() < t1.prevent_date) needs to become (CURDATE() < t1.end_date)
  4. for example SELECT t1.overwrite_prevent_ordering, t2.id, t2.title, t2.gender, t3.title AS employment_status, DATE_FORMAT(t1.start_date, '%Y-%m-%d') AS start_date, DATE_FORMAT(t1.end_date, '%Y-%m-%d') AS end_date, DATE_FORMAT(t1.early_date, '%Y-%m-%d') AS early_date, DATE_FORMAT(t1.prevent_date, '%Y-%m-%d') AS prevent_date FROM wearer_wardrobes t1 LEFT JOIN wardrobes t2 ON t1.wardrobe_id = t2.id LEFT JOIN employment_status_options t3 ON t2.employment_status = t3.id WHERE t1.wearer_id = $order_wearer_id AND ( (CURDATE() >= t1.start_date OR CURDATE() >= t1.early_date) AND ( (CURDATE() < t1.end_date AND t1.prevent_date IS NULL) OR (CURDATE() < t1.end_date AND CURDATE() < t1.prevent_date) ) )
  5. Thanks folks, I tried doing something similar Muddy_Funster and kind of got it working but it's when the overwrite_prevent_ordering = 1 comes into play. it needs to overwrite the prevent_date settings.
  6. Hi Muddy_Funster, Thanks for your reply. Yeah I know the if conditionals are not correct, I only tried to explain the logic like that as i'm not sure how else to do it. So what i'm trying to do is the following: The database has a long list of wardrobes Each wearer is assigned multiple wardrobes but with a date range of when they can view this wardrobe There might be some cases were they might be able to view this wardrobe before the start_date ie. early_date and some cases were they might be prevented from viewing the wardrobe before the end_date ie. prevent_date Hope that helps you understand
  7. Hi, I'm having trouble trying figure out this mysql query. the query is as follows SELECT t2.id, t2.title, t2.gender, t3.title AS employment_status FROM wearer_wardrobes t1 LEFT JOIN wardrobes t2 ON t1.wardrobe_id = t2.id LEFT JOIN employment_status_options t3 ON t2.employment_status = t3.id WHERE t1.wearer_id = $order_wearer_id if(t1.overwrite_prevent_ordering == 1 && t1.early_date != NULL){ AND (CURDATE() >= t1.early_date AND CURDATE() < t1.end_date) }elseif(t1.overwrite_prevent_ordering == 1){ AND (CURDATE() >= t1.start_date AND CURDATE() < t1.end_date) }elseif(t1.early_date != NULL && t1.prevent_date != NULL){ AND (CURDATE() >= t1.early_date AND CURDATE() < t1.prevent_date) }elseif(t1.early_date != NULL){ AND (CURDATE() >= t1.early_date AND CURDATE() < t1.end_date) }elseif(t1.prevent_date != NULL){ AND (CURDATE() >= t1.start_date AND CURDATE() < t1.prevent_date) }else{ AND (CURDATE() >= t1.start_date AND CURDATE() < t1.end_date) } ORDER BY t2.title ASC, t3.title ASC, t2.gender ASC Is this possible wth mysql or must I select all results and filter then out with php Thanks
  8. SELECT * FROM returns t1 LEFT JOIN returns_items t2 ON t1.id = t2.return_id WHERE (t1.order_date >= '2013-08-01') OR SELECT * FROM returns t1 LEFT JOIN returns_items t2 ON returns.id = returns_items.return_id WHERE (t1.order_date >= '2013-08-01') Same error only the unknown column 'returns.id' changes. If I change it to: SELECT * FROM returns t1 LEFT JOIN returns_items t2 ON returns_items.return_id = returns.id WHERE (t1.order_date >= '2013-08-01') The unknown column becomes 'returns_items.return_id'
  9. Table: returns --- id (primary) reference account_number order_date Table: returns_items --- id (primary) return_id qty gas_type SELECT * FROM returns t1 LEFT JOIN returns_items t2 ON t1.id = t2.return_id WHERE (t1.order_date >= '2013-08-01') Error is: #1054 - Unknown column 'returns.id' in 'on clause'
  10. Error is: #1054 - Unknown column 'returns.id' in 'on clause' And yes sorry order_date is in the returns table, i forget that when typing my message
  11. Hi, Get an unknown column error but the code looks fine, wondering if someone else can see anything i'm missing: Table: returns id (primary) reference account_number Table: returns_items id (primary) returns_id qty gas_type SELECT * FROM returns t1 LEFT JOIN returns_items t2 ON t1.id = t2.return_id WHERE (t1.order_date >= '2013-08-01') Any help much appreciated.
  12. Hi, I need help with mysql performance with large tables. I have the following tables: Person (8,021 Records) --- ID (Primary Key) Title (varchar) FirstName (varchar) Surname (varchar) ContactDetails (text) PersonCode (int) Booking (79,378 Records) --- ID (Primary Key) StartDate (datetime) FinishDate (datetime) BookingCode (int) RunCode (int) BookingTypeCode (int) PersonBooking (171,563) --- ID (Primary Key) PersonCode (int) BookingCode (int) IsDriver (varchar) It is taking 8.4983 sec just to display 31 results with the query below, anyone any idea how I could speed this up. Kind Regards. SELECT t1.BookingCode, t1.IsDriver, t2.RunCode, t2.Duration, t2.BookingTypeCode, DATE_FORMAT(t2.StartDate, '%a') AS StartDay, DATE_FORMAT(t2.StartDate, '%H:%i') AS StartTime FROM person_booking t1 LEFT JOIN booking t2 ON t1.BookingCode = t2.BookingCode WHERE t1.PersonCode = 21280 AND (t2.StartDate >= '2012-07-02' AND t2.StartDate < '2012-07-09') ORDER BY t2.StartDate
  13. Brilliant smoseley, cheers for the help
  14. Thanks for your reply. What about creating an array from the CSV and using that instead of querying the database?
  15. How would I go about doing the following: I have a csv like this I have a database structured like this Section is a sub division. What is the best way to get the information from CSV into this database? Should I have another table and store the CSV data as is and then query that to make the other tables. Any help much appreciated.
  16. Thanks for the reply. I've changed it to the following but it only returns 1 result from the array. Any ideas? Cheers $category_array = array(); function display_categories($parent, $level) { $conn = dbConnect('query'); $sql = "SELECT id, name FROM categories WHERE parent_id = $parent AND is_active = 1 ORDER BY sort_order DESC"; $result = @ $conn->query($sql); while($categories = $result->fetch_assoc()) { $array[] = array('id' => $categories['id'], 'name' => $categories['name'], 'level' => $level); display_categories($categories['id'], $level+1); return $array; } } $category_array = display_categories(0,0); var_dump($category_array);
  17. Hello there, I am having problem returning array from function the code is as follows: $category_array = array(); function display_categories($parent, $level) { $conn = dbConnect('query'); $sql = "SELECT id, name FROM categories WHERE parent_id = $parent AND is_active = 1 ORDER BY sort_order DESC"; $result = @ $conn->query($sql); while($categories = $result->fetch_assoc()) { $array = array('id' => $categories['id'], 'name' => $categories['name'], 'level' => $level); display_categories($categories['id'], $level+1); } } $category_array = display_categories(0,0); var_dump($category_array); Any help much appreciated. Thank
  18. Thanks you very much for all the help James. Much Appreciated.
  19. The property of 'balance' is float(10,2)
  20. Thanks james but that's not what i'm looking for. I added it to the database as 34.55 and I need it to come out as 34.55 so that if u update the value but adding 0.30, it can go back into the database as 34.85. The problem with using money_format() or number_format() is that it adds punctuation such as commas and full stops. Where does the 34.549999237061 come from?
  21. Thanks for the reply, I am working with a prebuilt system that uses float for currency. To retrieve the value is use the follow: <code> $conn = dbConnect('query'); $sql = 'SELECT id, balance FROM user WHERE id = ?'; $stmt = $conn->stmt_init(); if ($stmt->prepare($sql)) { $stmt->bind_param('i', $_GET['id']); $stmt->bind_result($id, $balance); $OK = $stmt->execute(); $stmt->fetch(); $stmt->free_result(); } echo $balance; </code>
  22. Yep, why is the 34.55 coming out of the database as 34.549999237061 and how do I make it display as 34.55
  23. Hello there, I'm having a problem displaying money correctly with php. I have a field called "balance" with the type float(10,2), in this field I have a number store as "34.55" which I can go in and look at within phpmyadmin but when I echo this value on the front-end of the site it is displayed as "34.549999237061" Can someone please help? Regards.
×
×
  • 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.