PHP Script: Make your own Thumbnail Generator via API
- Category: Personal Home Page
- Hits: 27884
Looking for an online tool that will take an image and make a thumbnail copy? This article is for me if I ever want to recreate an online tool capable of receiving an image URL and which both generates and outputs a thumbnail image and makes it downloadable via URL.
Why?
Performance. I have a client with about 10k images of products that they want to appear in a dropdown menu of a JavaScript widget. When the user first loads up the webpage containing the widget, 10k images are downloaded. I have put in a lazy loading process where it will load the first few images and as the user scrolls down the dropdown, more are loaded. This still gets buggy on certain mobile devices as some images are over 1Mb.
So I needed a tool that every time a new product is added, a thumbnail gets generated and stored in the same application. Trawling through the first few pages of Google, all the online tools that did the same had a pricing page. I can understand why you would need to limit users so I don't blame them. But if you can, why not make your own?
How?
So you will need a webserver of your own running PHP 8. The following PHP script was only tested using PHP version 8.x so I can't say whether it will work for previous versions.
I cannot take credit for this script, as I asked OpenAI's ChatGPT to write it initially, and to build upon it as I kept moving the goal posts, figuratively speaking, and changing the requirements. I am impressed however that every version it iterated, worked exactly as I asked with only 1 error where it mixed a method with a comment but thereafter, a working script every time.
PHP Issue: simplexml_load_string parser error : Input is not proper UTF-8, indicate encoding !
- Category: Personal Home Page
- Hits: 39172
What?
A quick article to stop me running into this issue again. This article serves to address the issue of importing characters from an XML in a different language character set and trying to load it in PHP with the function simplexml_load_string(). The error I get is something similar to:
PHP Warning:
simplexml_load_string(): Entity: line #: parser error : Input is not proper UTF-8, indicate encoding ! Bytes: 0xA0 0x3C 0x2F 0x73 in /home/public_html/my_folder/my_xml_processing_script.php on line 160
Why?
I'm downloading an XML feed to our servers, and then loading the downloaded file into memory with simplexml_load_string(). I get the above error when it is attempting to load an XML feed which is mostly in Spanish and breaks at the following XML node:
<baños>2</baños> -> yields issue: PHP Warning: simplexml_load_string(): <baños>2</baños> in /home/public_html/my_folder/my_xml_processing_script.php on line 160 should read <baños>2</baños>
How?
PHP - Remove newlines and spaces from StyleSheet
- Category: Personal Home Page
- Hits: 11078
What?
This is a quick note on how to reduce a whole bunch of CSS into a single line without unnecessary spaces and new lines.
Why?
What I have:
#copyright a{
margin: 10px 0 0 85px;
box-shadow: 5px 5px 5px 0px rgba(51, 51, 51, 0.3);
}
What I want:
#copyright a{margin:10px 0 0 85px;box-shadow:5px 5px 5px 0px rgba(51,51,51,0.3);}
How?
So I'm doing this with a regular expression to get rid of newlines:
$v_AppStyle = "
#copyright a{
margin: 10px 0 0 85px;
box-shadow: 5px 5px 5px 0px rgba(51, 51, 51, 0.3);
}";
$v_AppStyleFormatted = preg_replace('/\s+/', ' ', $v_AppStyle);
and a few str_replace arrays:
// exceptions
$a_ReplaceFrom1 = array("px ", "0 ", " a");
$a_ReplaceTo1 = array("px?", "0?", "?a");
$v_AppStyleFormatted = str_replace($a_ReplaceFrom1, $a_ReplaceTo1, $v_AppStyleFormatted);
// replace all spaces to empty and replace question marks back to spaces
$a_ReplaceFrom2 = array(" ", "?");
$a_ReplaceTo2 = array("", " ");
$v_AppStyleFormatted = str_replace($a_ReplaceFrom2, $a_ReplaceTo2, $v_AppStyleFormatted);
echo $v_AppStyleFormatted;
Convert Past Date to Time Ago in PHP
- Category: Personal Home Page
- Hits: 22819
Just a quick note to refine a function that will take a date in the past and return the number of years, months, weeks, days, hours, minutes and seconds.
Why?
Here are some examples of what we want to achieve:
1 year 2 months 3 weeks 4 days 5 hours 6 minutes 7 seconds // full string 1 year 3 weeks 5 hours 7 seconds // where shown values are not zeroActually we just want the first two words of those strings:
3 days ago // where event is 3 days ago or just over but less than 4 days ago 1 week ago // where event is just over 1 week ago but less than 2 weeks ago
How?
URL Alias uniqueness with PHP & MySQL
- Category: Personal Home Page
- Hits: 26692
So this is an article for me on how to copy Joomla's and Wordpress' feature where a Title/Name value is converted to a search-engine friendly URL alias and is unique in the database. Ok that's a long sentence; let me try this:
- When I save a record in JoomlaCMS or WordpressCMS
- It creates a unique name to use in URLs
- I want that
Why?
These are used as inputs to server-side scripts for the sake of search-engine friendliness. There are no silver bullets here but I want to block any character that isn't a letter, a number or an underscore.
How?
Suppose the following exists as a MySQL database table called my_table_name:
What we have:
/----------|-------------------|----------------------\ | id | name | url_alias | |----------|-------------------|----------------------| | 1 | My *First* Test | my_first_test | | | | | | | | | \----------|-------------------|----------------------/What we want:
/----------|-------------------|----------------------\ | id | name | url_alias | |----------|-------------------|----------------------| | 1 | My *First* Test | my_first_test | | 2 | My *First* Test | my_first_test_1 | | 3 | My _-_First Test | my_first_test_2 | \----------|-------------------|----------------------/
Foreign Characters create symbols in PHP and MySQL
- Category: Personal Home Page
- Hits: 30508
- PHP 5.4
- MySQL 5.1.4
- Databases initially setup with collation 'latin1_swedish_ci'
What
Another article trying to help people display foreign characters on their website without the funny question marks in diamond symbols and how I solved it in my case.
Convert XML UTF-16 to JSON UTF-8 with PHP cURL
- Category: Personal Home Page
- Hits: 52703
Fix PHP cURL: parser error: Document labelled UTF-16 but has UTF-8 content
What?
This is an article with notes for me on how to convert some received XML encoded in UTF-16 to some JSON in UTF-8. If it were entirely in UTF-8, I would simply load the received XML with SimpleXML and use the built-in PHP JSON_encode function. I ran into the following errors:
Warning: SimpleXMLElement::__construct() [<a href='simplexmlelement.--construct'>simplexmlelement.--construct</a>]: Entity: line 1: parser error : Document labelled UTF-16 but has UTF-8 content in /public_html/.../.../my_script.php on line ###
Warning: simplexml_load_string() [<a href='function.simplexml-load-string'>function.simplexml-load-string</a>]: Entity: line 1: parser error : Document labelled UTF-16 but has UTF-8 content in /public_html/.../.../my_script.php on line ###Why?
So I've googled, binged and yahoo'd for this and although there are some solutions that deal with loading UTF16 content into SimpleXMLElement or simplexml_load_string, it doesn't solve my problem. I'm receiving XML data within a cURL result but I get the above error with using either "SimpleXMLElement" or "simplexml_load_string". Returning the XML with cURL isn't a problem, but I want to convert it to JSON and I usually use a PHP function to load the data into an XML array and use the built-in PHP function: "json_encode".
How?
PHP: First name and Initial of Surname
- Category: Personal Home Page
- Hits: 16415
A note for myself on some code to convert a string of two names into a string made up of the first name and then using the initial of the second name.
-- What I have John Smith Fred.Bloggs -- What I want John S. Fred B.
How?

