PHP checks whether an image exists and the loading event in jquery processes the image

Source: Internet
Author: User
In the company's image server, the same product usually has a corresponding big image and thumbnail. therefore, when developing mobile web websites, we use product image thumbnails by default, and retrieve the thumbnail path when querying the database. however, for some reason, from time to time, test colleagues will always find us and say that the product image is not shown. It is a bug and requires

In the company's image server, the same product usually has a corresponding big image and thumbnail. therefore, when developing mobile web websites, we use product image thumbnails by default, and retrieve the thumbnail path when querying the database. however, for some reason, from time to time, test colleagues will always find us and say that the product image is not shown. It is a bug and requires

In the company's image server, the same product usually has a corresponding big image and thumbnail. therefore, when developing mobile web websites, we use product image thumbnails by default, and retrieve the thumbnail path when querying the database. however, for some reason, from time to time, test colleagues will always find us and say that the product image is not shown. It is a bug and needs to be modified.

So, the problem arises.

(1) How does the PHP background determine whether images on the remote server exist?

Solution: Get the image path --> determine whether the image exists --> not exist. Use the big image

/*** @ Desc check whether the remote image exists * @ param string $ url: the remote address of the image * @ return boolean $ found is true; otherwise, false */function check_remote_file_exists ($ url) {// curl initialization $ curl = curl_init ($ url); // curl_setopt ($ curl, CURLOPT_NOBODY, true) without retrieving data; // send a request, receive result $ result = curl_exec ($ curl); $ found = false; if ($ result! = False) {// check whether the http response code is 200 $ statusCode = curl_getinfo ($ curl, CURLINFO_HTTP_CODE); if ($ statusCode = 200) {$ found = true ;}}// close the curl resource curl_close ($ curl); // return the result return $ found;} $ url = 'HTTP: // exist (check_remote_file_exists ($ url); echo'
'; Var_dump (check_remote_file_exists ($ error_url);/* output of the preceding example: bool (true) bool (false )*/

It can be seen that the CURL series functions in PHP are still very powerful.

When I was in Baidu, I also found that the fopen () function was used for judgment, but the experiment failed to implement the corresponding function. For example:

1 $ url = 'HTTP: // home.baidu.com/resource/r/home/img/logo-yy.gif'; 2 $ error_url = 'HTTP: // when 3 4 if (@ fopen ($ error_url, 'R ')) 5 {6 echo 'file Exits '; 7} 8 else 9 {10 echo 'file Do Not Exits'; 11} 12/* 13 and above example output: File Exits14 */

(2) processing images using the load event in front-end jquery

Further Thinking, can the front-end use jquery to determine whether a remote image exists? Unfortunately, when I wrote this article, I failed to find a way to judge.

Example:

Note: Introduce jquery. js

1234
 5Load event6789 101125

When using the load event, pay attention to the following two points:

First, the load event and the current jquery code execution sequence are asynchronous,

Second, $ (document). ready () refers to the completion of html code loading, and the load event refers to the completion of the requested image download.

Http://www.cnblogs.com/kinger/p/4292412.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.