Jump to content
Larry Ullman's Book Forums


  • Content Count

  • Joined

  • Last visited

Community Reputation

2 Neutral

About Max

  • Rank
    Advanced Member
  1. The words are fact. I will look at including regular exressions in the glob(). Regards Max
  2. Further to the above comment, I have stumbled across something called 'imagick' which is part of the PHP domain. The PHP manual says something like this: bool Imagick::resizeImage ( int $columns , int $rows , int $filter , float $blur [, bool $bestfit = false ] ) ..and gives examples like this: <?php function resizeImage($imagePath, $width, $height, $filterType, $blur, $bestFit, $cropZoom) { //The blur factor where > 1 is blurry, < 1 is sharp. $imagick = new \Imagick(realpath($imagePath)); $imagick->resizeImage($width, $height, $filterType, $blur, $bestFit); $cropWidth = $imagick->getImageWidth(); $cropHeight = $imagick->getImageHeight(); if ($cropZoom) { $newWidth = $cropWidth / 2; $newHeight = $cropHeight / 2; $imagick->cropimage( $newWidth, $newHeight, ($cropWidth - $newWidth) / 2, ($cropHeight - $newHeight) / 2 ); $imagick->scaleimage( $imagick->getImageWidth() * 4, $imagick->getImageHeight() * 4 ); } header("Content-Type: image/jpg"); echo $imagick->getImageBlob(); } ?> ...which I can't get to work. Larry - perhaps this could be a subject for your next book? Last time I suggested a book, you brought out the excellent (but headache inducing ) Javascript - Develop and Design, which I was pleased to buy but had to read about 5 times for it to start to sink in. Old dogs and new tricks??? I'm sure it would be a good seller. You certainly have a way of explaining very complex subjects.
  3. Another question for Larry. I would like to offer a library of all of the thousands of images that I have in my raw data directory by displaying thumbnails. I know I can use: <a href = "images/raw/yadayada.jpg" target = "_blank"><img style = "width: 100px;" src = "images/raw/yadayada.jpg" .... /> </a> but this will involve huge overheads as it will mean downloading approx 1Mb images and then resizing them in the browser, so I would prefer to resize the images in PHP before downloading them, but I can't find any reference to this in the PHP manual, or the PHP GD manual. Any advice would be much appreciated. Max
  4. Just spotted a problem with the script - it is only checking files with the extension .jpg, not .JPG. Have changed: //Now create a variable ($getlisting) to be able to call only the category that we are interested in... $getlisting = 'images/' . $category . '/*.jpg'; to: //Now create a variable ($getlisting) to be able to call only the category that we are interested in... $getlisting = 'images/' . $category . '/*.*'; - can't really see any neat way around it. I suppose I could run it twice, once with .jpg and then with .JPG, then run the .JPGs through strtolower and then combine them in the $getlisting array to then be compared with the raw files. Any comments please.
  5. Hi Larry I thought that I'd post this as it might help other php-ers. I have a website which has many images. To facilitate the download of images, I save a reduced image under a directory name e.g. images/activities/P3451098.jpg for thumbnails and slideshows as well as the full size in e.g. images/raw/P3451098.jpg. Then I can offer an enlarged image using <a href = ***><img src...... target = "_blank"> etc. The problem is that as I send the images up using FTP (Filezilla) I can't be sure if all of the reduced images have the corresponding full sized images thanks to advanced dementia. Therefore, I have written a short routine to compare the two directories.... COMMENTS: 1) I was confused at first as to why it didn't work properly until I discovered that glob() is case sensitive, so that it wasn't finding .JPGs so had to run it twice (there is a variety of both depending on which camera they came from). 2) The search routine is not very elegant as it runs through all of the raw files even though it may have found a match in the first file. 3) I know that I could use glob(images/raw/*.*) instead of glob(images/raw/*.*) and in this case it wouldn't make any difference as all files in these directories are .jpgs - it just seems more precise to use *.jpg. I would appreciate any comments from anybody..... <?php //Declare variable $found to avoid undefined variable errors $found = "No"; //Call header which handles security and error messages include ('adminheader.php'); //Create an array $images which lists all of the relevant image directories on the server.. $images = array('4X4', 'activities', 'almonds', 'alojamiento', 'altiplano', 'archery', 'area', 'artifacts', 'baza', 'castillejar', 'castril', 'caving', 'fiestas', 'flowers', 'history', 'huescar', 'kayaking', 'index', 'markets', 'mountaineering', 'mountains', 'museums', 'photography', 'puebla', 'rafting', 'village', 'wildlife', 'activities/activities', 'activities/caving', 'activities/kayaking', 'activities/mountaineering', 'activities/outdoors', 'activities/rafting'); //Create array $raw which will be a listing of all of the large image files on the server $raw = glob('images/raw/*.jpg'); $raw2 = glob('images/raw/*.JPG'); //Work through the $images array so as to compare whether there is a large matching file in the 'raw' directory... foreach($images as $key => $category) { //Start with a header to ID each category.... echo 'CATEGORY:... ' . $category . '<br /><br />'; //Now create a variable ($getlisting) to be able to call only the category that we are interested in... $getlisting = 'images/' . $category . '/*.jpg'; //create an array of the files in this category.. $listing = glob($getlisting); //Run through the $raw array to see if it contains a file that matches the file in the category... foreach($listing as $key => $value) { //Create variable to use in str_replace.. $directory = 'images/' . $category . '/'; // Remove the unwanted directory details.... $value = str_replace($directory, "", $value); //Reduce any JPGs to jpgs... $value = strtolower($value); //Run through the $raw array to find a match.. foreach($raw as $key => $rawvalue) { //Get rid of directory details from $rawvalue... $rawvalue = str_replace("images/raw/", "", $rawvalue); //Bring any .JPGs to .jpgs.... $rawvalue = strtolower($rawvalue); //Do comparison. If raw image matches reduced image then do nothing.. if ($value == $rawvalue) { //If the small and large files match then switch variable $found to 'on'.... $found = "Found"; } } //Run through the $raw array to find a match.. foreach($raw2 as $key => $rawvalue) { //Get rid of directory details from $rawvalue... $rawvalue = str_replace("images/raw/", "", $rawvalue); //Bring any .JPGs to .jpgs.... $rawvalue = strtolower($rawvalue); //Do comparison. If raw image matches reduced image then do nothing.. if ($value == $rawvalue) { //If the small and large files match then switch variable $found to 'on'.... $found = "Found"; } } //If small and large files don't match, then print out the filename .. if ($found != "Found") { echo $value . "<br /><br />"; } //reset $found to "Not found".. $found = "No"; } //Close individual Category foreach echo "<br /><br />"; } //Close all category foreach echo "ALL DONE!"; include ('footer.php'); ?>
  6. STOP PRESS!! Darn it! I have just found a much easier way, thanks to your book (Chapter 2):.... Create array of all jpg files on server in the images/activities/activities directory: $listing = glob("images/activities/activities/*.jpg"); Use 'shuffle() to change the order of the jpg files in the array: shuffle($listing); Use 'foreach()' to trawl through the array to extract the filenames: foreach($listing as $key => $value) { This is a bit of js: echo '<div class="mySlides fade"> <img src = "' . $value . '" style=" display: block; margin: auto; width:80%; border-style: solid; border-width: 2px; border-color: black; vertical-align: top;" alt = "Sorry - image missing." /> </div>'; echo '<div style="text-align:center"> <span class="dot"></span> </div>'; $repeat = 1; } That also solves the issue of repeats. KISS - keep it simple, STOOPID! Regards Max
  7. Hi Larry. I am sure that this has been done elsewhere, but I thought I would share it with you anyway. I have a website that displays a slideshow of images of where I live ( www.beautifulcastril.com ). To make things cleaner and quicker, I just upload the jpgs to the server via Filezilla and then, in the program use glob() to bring their filenames into an array. I then used to pass through the array using foreach() to display the images automatically using js and CSS as per your book Chapter 2. The irritating thing was that they always came out in the same order so I have changed the php to randomize the image order. I have added comments in red specifically for this post:.... <?php include ('header.php'); ?> <table style = "width: 100%; border: 0; table-layout: fixed;"> <tr><td style = "width: 20%"><a href="castril.php"><img src = "images/village/P9040321.jpg" style = "width: 100%;" alt = "Sorry - image missing" title = "Click here" /></a></td><td style = "width: 20%;"><a href="castril.php" id = "area1">Castril town</a></td><td rowspan = "6" style = "vertical-align: top; margin: auto;"> This starts the js/CSS: <div class="slideshow-container" style = "margin: auto;"> <?php Get the images from the server and put them into an array called '$listing' using glob(): $listing = glob("images/area/*.jpg"); Find out how many files there are and subtract 1 as they start with 0: $arrlength = count($listing) - 1; Do a for/next loop to run through the whole array (from 0 to the length of the array - 1: for($x = 0; $x < $arrlength; $x++) { Get a random number between 0 and array length - 1 using mt_rand() which apparently is quicker than rand(): $random_keys = mt_rand(0, $arrlength); $value is the filename related to the random number obtained above: $value = $listing[$random_keys]; Use some js to take the filename and produce the image (see below): echo '<div class="mySlides fade"> <img src="' . $value . '" style="display: block; margin: auto; width:80%; border-style: solid; border-width: 2px; border-color: black; vertical-align: top;" alt = "Sorry - image missing"> </div>'; echo '<div style="text-align:center"> <span class="dot"></span> </div>'; $repeat = 1; //Can't remember what this does!!! } ?> </div> <br> Now for the js: <script> var slideIndex = 0; showSlides(); function showSlides() { var i; var slides = document.getElementsByClassName("mySlides"); var dots = document.getElementsByClassName("dot"); for (i = 0; i < slides.length; i++) { slides.style.display = "none"; } slideIndex++; if (slideIndex> slides.length) {slideIndex = 1} for (i = 0; i < dots.length; i++) { dots.className = dots.className.replace(" active", ""); } slides[slideIndex-1].style.display = "block"; dots[slideIndex-1].className += " active"; setTimeout(showSlides, 3000); // Change image every 3 seconds } </script> etc. etc....... COMMENTS: 1) I know that my CSS is clumsy and I should use id = "..." linked to a CSS file to make it cleaner and not be passing the same CSS code to the browser. Will sort it. 2) The mt_rand() function doesn't check that it hasn't supplied that number before and perhaps I should add a routine that looks at images that have been displayed and to inhibit the repeat(s) but it would make the code a bit clunky, I think. Something like: - Create an array In the 'for' loop: Second loop to: - Pass through array to see if $random_keys == $myarray() - If 'yes' then delete $random_keys - If 'No' then add $random_keys to array Return to loop...... In reality, if there are more than a few images, it doesn't really notice. I hope this has been of help. Regards Max
  8. That's a great question, Larry. Actually, every other website that I have looked at just terminates the text irrespective of where there is a space. e.g. Lovely property, not overpriced at all, new ro..........more... I have a bad habit of over-thinking these things. All sorted now.
  9. Thanks, Larry - I think I have my head round it now - that one apostrophe entered through a textarea caused me a lot of chaos. Keep up the great work.
  10. Following your book, I have now added mysqli_real_escape_string() to the php which actually works for everything except `` and ´´ which it doesn't escape, but I will take the risk of those never being used by the people who are uploading data. They are not the standard apostrophes but Hex 60. To get to the root of the problem, what I did was to comment out the steps whereby I uploaded the data to MySQL and instead did an 'echo' instead: e.g. $update = "UPDATE blah SET Name = '$name', Address = '$address', Description = '$desc' WHERE Ref = $ref LIMIT 1"; echo $update; I then ran the script and copy/pasted the result from the screen into the phpMyAdmin 'SQL' service and found that it failed. On investigation, it was the apostrophe causing th problem. I hope that this helps others.
  11. In addition, I have been caught out with uploading text to MySQL from a textarea - when people use an apostrophe - as in "I've" - then the MySQL sees that apostrophe as the end of a data set as in - UPDATE blah SET Comments = 'blah blah it's blah blah' you find that MySQL has thrown it all out because it has seen the " ' " as a termination character. The problem is that there are so many apostrophes on a keyboard - ' , ` , ´ , " etc - so I have included a str_replace routine to turn these into spaces as \' doesn't seem to work in all circumstances.
  12. Hi Larry Comments genuinely meant - and if I am mad enough to scramble my brains by learning something new I will always look for your books on that subject. Regarding the character encoding, I am referring to a textarea such as this one. Head is set to: <meta charset="utf-8"> and the MySQL "utf8_unicode_ci". On my keyboard are characters such as an "enye" - the n with a tilde on top. Also, it would be nice for people to have a way of writing m2 as for 'square metres'. When I put the 'enye' into MySQL using phpMyAdmin it works and reads correctly, but when read by html in my web page it comes out as '�'. I guess the obvious thing for me to do is use the same software as is used on this forum which looks like "ipsEditor" (Invision Power). For example, you have X2 and X2 on the control panel. To keep it simple, I guess that I could put instructions under the textarea field saying "To insert superscript 2 type ' ² '" as most people don't have superscript 2 on their keyboard anyway. Here's a quick and dirty program I wrote to test it: <!doctype html> <html lang="en"> <head> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta charset="utf-8"> <!--[if lt IE 9]> <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script> <![endif]--> </head> <body> <?php echo '<h2>'; echo 'This is the raw data: Ñ'; echo '<br /><br /><br />'; echo 'This is using HTML entities: Ñ'; echo '<br /><br /><br />'; $ent = "Ñ"; echo 'This is using htmlentities: ' . htmlentities($ent); echo '</h2></body></html>'; If I run this on the server, I get the following: This is the raw data: � This is using HTML entities: Ñ This is using htmlentities: Best regards Max
  13. Hi again Larry, Character encoding....Hmmmm If I write HTML, and want to include an ampersand -> & - or a euro sign -> € or alternatively € I type exactly those characters. The question is when one is using php to write text to mySQL. For example, if you have a web page which invites a customer to upload text to the database. If they use a keyboard that has unusual characters such as a half, which I would type in HTML as ½ it is not saved as such even though the DB has UTF-8 encoding. So...what happens when the text is retrieved from the DM and sent to the browser is that you get the ?inside a black diamond symbol. I have read your book a milion times (as well as your excellent javascript tome) and just can't get my head round it. I suppose I could write a load of turgid php such as 'str_replace....' for each character we are likely to come across. Any advice would be much appreciated. Interestingly, the Daily Mail website forum has the same problem, which doesn't make me feel any better, to be honest!! Thanks again for all your time spent on us dunces! P.S. Another question - how would you offer the client the option to put into text, say, a superscript 2 in the sense of metres squared - or in the USA - feet squared when there is no key for that? Sorry for the spelling in the title!!
  14. Hi Abigail Thanks for that. My provider doesn't offer e-mail help - you have to phone them and I was hoping to avoid the embarrassment of admitting that I'd done something stoopid !! The web site will be back tomorrow (did before when I screwed up). I would be nice to be able to kill it through php some way. I have tried most things - logging out, uploading (FTP) a safe version of the program. Regards Max
  15. Larry, what would we do without you??? How about something like this??: //We'll call the variable that contains the text '$my_text' // html elements are not all the same size, eg <h1>, </strong>, <small> // Do a 'while...'....then.... //Find the first opening tag (<) $first = strpos("<") //Find first closing tag (>) $second = strpos('>',$my_text) $length_of_string = $second - $first; //Remove the offending element substr_replace($my_text, "", $first, $length_of_string) //end while Unfortunately, this removes all of the HTML formatting. I have found this: html_entity_decode() Function which I'm going to have a play with to see how well it works - I'll report back. Regards Max
  • Create New...