PHP determines if a picture exists and the Load event in jquery handles the image

Source: Internet
Author: User
Tags response code

In the company's image server, the same product will generally have a corresponding large map and thumbnails. Therefore, when we develop the mobile Web site, the default is to use a thumbnail image of the product, to query the database is the path of the thumbnail. But, for some reason, every once in a while, a test colleague will always find us, said the product image is not shown, is a bug, need to modify.

So, here's the problem.

(1) How does PHP background determine if a picture exists on a remote server

Solution: Get Picture path---------------No, no, use large image

/** * @desc check if remote picture exists * @param string $url picture remote Address * @return Boolean $found exists as true, otherwise false*/functionCheck_remote_file_exists ($url) {//Curl Initialization$curl= Curl_init ($url);//do not retrieve datacurl_setopt ($curl, Curlopt_nobody,true);//send request, receive result$result= Curl_exec ($curl);$found=false;if($result!==false) {//Check if the HTTP response code is$statusCode= Curl_getinfo ($curl,curlinfo_http_code);if($statusCode= = 200) {$found=true;}}//Turn off Curl ResourcesCurl_close ($curl);//return Resultsreturn $found;}$url= ' Http://home.baidu.com/resource/r/home/img/logo-yy.gif ';$error _url= ' Http://home.baidu.com/resource/r/home/img/logo-yy111111.gif ';Var_dump(Check_remote_file_exists ($url));Echo' <br/> ';Var_dump(Check_remote_file_exists ($error _url));/*above example output: BOOL (TRUE) bool (false)*/

As you can see, the Curl series function in PHP is still very powerful. When I was in Baidu, I also found that using the fopen () function to judge, but after the experiment,failed to implementThe corresponding function. For example:
1 $url= ' Http://home.baidu.com/resource/r/home/img/logo-yy.gif ';2 $error _url= ' Http://home.baidu.com/resource/r/home/img/logo-yy111111.gif ';3  4 if( @fopen($error _url, ' R ' ) )5 {6 Echo' File Exits ';7 }8 Else9 {Ten Echo' File does not Exits '; One } A /* - above example output: File Exits - */

(2) Using the Load event in front-end jquery to further consider the image processing, can the front-end use jquery to determine whether the remote image exists? Unfortunately, when writing this article, the method of judging cannot be found. We know that if a picture doesn't exist, The img tag displays a torn small pattern by default, and the customer experience is very unfriendly, and we can use the Load event in jquery (triggered when the picture is loaded) to process the picture. Idea: Hide all pictures First--Add Load Event to img tag--Picture loading complete , an example of the display image is as follows: Note: Introduction jquery.js
1 <!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en">2 <HTML>3 <Head>4 <Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8">5 <title>Load Event</title>6 <Scriptlanguage= "JavaScript"src= "Jquery.js"></Script>7 </Head>8 <Body>9 <imgsrc= "Http://home.baidu.com/resource/r/home/img/logo-yy.gif"alt=""ID= ' Test '/>Ten </Body> One <Script> A //jquery Load Event test - //var h=$ (' #test '). Height (); - //document.write (h); the $('img'). each (function(){ - //hide the picture first - $( This). Hide (); - //Monitoring the Load event + $( This). Bind ('Load',function(){ - //load complete, display picture + $( This). Show (); A }); at }); - </Script> - </HTML>

There are two points to note when using the Load event: first, the Load event and the current jquery Code execution order is asynchronous, second, $ (document). Ready () means that the HTML code is loaded, and the Load event refers to the requested picture download complete. If the IMG tag is not set to width and height, the default is not loaded when the width of the IMG is 0, that is, width=height=0, using this feature, we can further processing the IMG tag, for example: http://www.cnblogs.com/ Kinger/p/4292412.html

PHP determines if a picture exists and the Load event in jquery handles the image

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.