Your SlideShare is downloading. ×
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
PHP Without PHP—Confoo
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

PHP Without PHP—Confoo

7,701

Published on

An obscure but ubiquitous design pattern in PHP development is known as Funky Caching. Using real architectural examples as a lens to look at this one simple PHP design pattern, we see how we can …

An obscure but ubiquitous design pattern in PHP development is known as Funky Caching. Using real architectural examples as a lens to look at this one simple PHP design pattern, we see how we can design web architectures that are "organic, democratic, and lasting/"

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
7,701
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
111
Comments
0
Likes
2
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide
  • An obscure but ubiquitous design pattern in PHP development is known as Funky Caching. Using real architectural examples as a lens to look at this one simple PHP design pattern, we see how we can design web architectures that are "organic, democratic, and lasting…"
  • Two prologues
  • Often attributed to Rasmus, but actually invented by Stig First you create an ErrorDocument line in your apache.conf. What this does is tell the webserver to redirect all missing files to a PHP script called “error.php” in your directory root. You then create this handler: Reference: http://derickrethans.nl/errorhandling/rasmus-trick.html http://lerdorf.com/tips.pdf
  • What does this trick do? Basically, when a file doesn’t exist, it gives an opportunity for PHP to create the data and return it instead of a 404 error page. The code above is pretty canonical, the only exceptions are where you branch to show the default error page… syntax highlighting code: php_value highlight.string '#b02c04' php_value highlight.comment '#8e98a6'php_value highlight.keyword '#96ad53'#php_value highlight.bg '#f1f7d7'php_value highlight.default '#524e37'php_value highlight.html '#95ad53'
  • … and where you generate the actual data to render to the user.
  • At the bottom is where the magic happens. It places the generated file directly into the web server path. What this means is that the next request to the same web resource goes directly for that resource without starting up this code and regenerating the data. Reference: 404 defined: http://en.wikipedia.org/wiki/HTTP_404 404 discussion: http://www.alistapart.com/articles/pe rfect404/ 404 examples: http://www.plinko.net/ 404/ HTTP codes: http://www.urbandictionary.com/define.php?te rm=404
  • PHP never gets instantiated after the first request! At that point it truly becomes PHP without PHP.
  • About the book and the metaphor. The foundation of Steve McConnell’s seminal text, Code Complete , was that software development should be based around the paradigm of construction. But that was fundamentally flawed because of the mythical man-month —the “man-mon th” term itself originally coming from construction work. We now know McConnell was wrong and software isn’t construction, it is engineering . And we’re called “software engine ers,” not “software workers” for this reason: we’re engineers, not construction workers.
  • Rhe foundation of Steve McConnell’s seminal text, Code Complete , was that software development should be based around the paradigm of construction. But that was fundamentally flawed because of the mythical man-month —the “man-mon th” term itself originally coming from construction work. We now know McConnell was wrong and software isn’t construction, it is engineering . And we’re called “software engine ers,” not “software workers” for this reason: we’re engineers, not construction workers.
  • Software as Engineering vs. other. My title at Tagged is currently “software architect.” And I have a “radical” idea that maybe titles are that way because they mean something. Meaning that if I’m hired as a software architect then I should think like an architect and find my inspiration from architecture.
  • So lets find our inspiration from architecture and see how it explains the Funky Caching pattern through three buildings. the first is one known as Fallingwater. References: Fallingwater logo from: http://www.fallingwater.org /
  • In Eastern mountain range of the United States there is a house that is CANTILEVERed 40 feet above a waterfall. This is one of the most famous buildings by American architect, FRANK LLOYD WRIGHT. In 1991 it was voted as the best all-time work of American architecture by the American Institute of Architects. Even though it is OVER 80KM from the nearest city people over a MILLION PEOPLE have traveled to see it. Reference: Image from http://commons.wikimedia.org/wiki/File:Falling_Water_01.jpg Wikipedia: http://en.wikipedia.org/wiki/Fallingwater Frank Lloyd Wrigh t: http://franklloydwright.org/Home.html
  • Yes, including me as a kid. The day it was built, TIME MAGAZINE wrote about it. Because it was a quintessential example of ORGANIC ARCHITECTURE of FLW. References: Der Spiegel: http://en.wikipedia.org/wiki/Der_Spiegel
  • A staircase connects the house directly to the stream below . The stream and waterfall, although not seen, can be heard in all parts of the house. The HATCHWAY that consists of SLIDING GLASS DOORS and the bottom step has no support from below. (FLOAT above like the water floats) References: http://wright-house.com/frank-lloyd-wright/fallingwater-p ictures/06E-hatchway-fallingwater.html
  • WINDOWS WRAP AROUND THE ENTIRE BUILDING so that the building MERGES with the surrounding GREENERY. Similarly the TUNGSTEN LIGHTING and the glasses compliments the FALL FOLIAGE of the region…
  • This is the VIEW that FLW planned for the house. References: http://wright-house.com/frank-lloyd-wright/fallingwater-pictures/F1SW-fallingwater-in-fall.html
  • Panes of glass are caulked directly to each other and to the steel frame or stone walls to further symbolize the lack of barriers between the construction and the surrounding nature. References: http://wright-house.co m/frank-lloyd-wright/fallingwater-pictures/34SE-guest-house-window.html
  • The driveway trellis has a semi-circular cutaway to accommodate an existing tree . References: http://wright-house.com/frank-lloyd-wright/fallingwater -pictures/15-tree-in-trellis-fallingwater.html
  • The stone floor in the living room , quarried from local rocks http://wright-hous e.com/frank-lloyd-wright/fallingwater-pictures/livingroom-fallingwater.html
  • The rock in the foreground is is an original boulder not disturbed from it’s original position. References: http://wright-house.com/frank-lloyd-wright/fallingwater-pictures/7-living-room-fireplace-hearth.php
  • From details like these to the whole view taken in at once, one gets a feeling that, in spite of the SHARP HORIZONTAL AND VERTICAL LINES of the building, the whole lives in harmony with its environment "instead of lording above [it] in an isolated spot as a man-made imposition.” Why is this building as recognizable as any NATURAL WONDER?
  • Surveyed the grounds before building. Organic: "form follows function." Wright argued that "form and function are one." Democratic “design should accommodate needs” Plasticity: “physical element should remove barriers.” Continuity “element should be part of nature”
  • How prevalent is funky caching? Every time you visit PHP.NET, you use it (need to get the order of needle-haystack by typing http://php.net/strstr ). A cynic would say, because PHP is so SLOW TO EXECUTE, it needs solutions like this to perform well. The problem with this argument is that NO WEB LANGUAGE OUTPERFORMS STATIC SERVERS. Becase it trades off speed of execution with SPEED FOR EASE OF DEVELOPMENT. As Andrei mentioned e arlier , that is fundamental to its design. In fact, all dynamically-typed scripting languages make this tradeoff.
  • The ubiquity of this trick in the PHP world is because it—like Frank Lloyd Wright's Fallingwater—lives in harmony with its environment. The environment is an APACHE WEB SERVER, persistent data store in the form of a RELATIONAL DATABASE, and the demands of large-scale, CONSUMER-CENTRIC web sites. Would this solution exist without an ErrorDocument handler built into Apache? Would this solution exist if we didn't persist content on a (relatively) slow data store like a database? Would this solution exist if the consumer didn't demand millisecond response time for dynamic content?
  • The language itself. PHP is a component of A WHOLE SYSTEM. It’s glue code.
  • developer time and resources
  • shared-nothing architecture
  • designed to solve the web problem Could this "ugly, but extremely functional and convenient" web language have been built to solve anything other than the ugly web problem?
  • One block from where my mother used to work, on the corner of Fifth Avenue and Bellefield in Pittsburgh, stands a strange sight…
  • References: Map of Pitt: http://www.tour.pitt.edu/documents/map200506.pdf
  • If you weren’t in PITTSBURGH IN THE LATE 1980’s you wouldn’t have understood how his could have happened . On this ground once stood the original Bellefield church built in the 1880’s. Since its CONGREGATION HAD BEEN MOVED further down Fifth Avenue, the building was sold a century later to developers trying to exploit the new business attracted by the PITTSBURGH SUPERCOMPUTING CENTER. They wanted to level it and build a new building, but were blocked when people mobilized to SAVE THE OLD TOWER. The developer then proceeded to honor this by DEMOLISH EVERYTHING BUT THE TOWER and building the ironically-named “Bellefield Towers” next to it.
  • This is the current Bellefield Church and an example of city architecture of which the tower is one of them…
  • The center is the county courthouse and jail, the top right is the local Korean church, and the bottom right is a private residence in the same neighborhood.
  • The photo at the top is was taken from the Cathedral of learning (previous slide).
  • When faced with a gorgeous 100-year old example of the area’s architecture, INSTEAD OF WORKING WITH THE ENVIRONMENT like Frank Lloyd Wright did with Fallingwater—in the same area of Pennsylvania no less!—the developer simply sawed it off! The CURVED MODERN ARCHITECTURE of the TOWERS of the latter serves only to emphasize how it was built with no consideration of the surrounding environment. I GREW UP WATCHING THIS HAPPEN and it is a LIFE LESSON IN ARCHITECTURAL HUBRIS.
  • “ I’m a developer, I can make the software conform to my needs.”Oh really? That sounds a lot like trying to “lord over the environment with an isolated man-made imposition.” “But what I mean is it’s all man-made in software, there is no environment.”
  • cover cube story, (where cubes are good- Yahoo, sales, user QA), cover waterfall process
  • When I joined my current company, they had a couple services written in Java, only Zend Accelerator could code cache their PHP 4 installation, Oracle RAC power ed the bac k-end and development occurred by engineers working in cubes with a relatively heavyweight waterfall development process.Even though I prefer Python to Java for services, we’ve increased our Java development to around 40% of our code base! Even though I prefer MySQL to Oracle, we still use Oracle as our back-end. Even the transition to the open office occurred after it became apparent the company had outgrown the cube-space.
  • Book on management by the founders of 37 Signals. Their last book was “Getting Real” about web business.
  • - cliche ridden with few examples “Hannah Montanta” reach for you dreams - “would rather be celebrities than businesspeople.” - 'Meetings are toxic.' In reality, good meetings are good and bad meetings are not good (generalization) - “Planning, they say, is not recommended” - only applies to their business - “They need to stop behaving like children.” - “Outside money is plan”, about why you should never take on third-party investors.37signals reveals that it took money from Bezos Expeditions, the VC company that invests on behalf of the Amazon founder, Jeff Bezos, in 2006.
  • Why? Because that is the environment and your solutions have to work within that environment. Anything else is architectural hubris. Let’s say it is the early days of social networking and you join a company that is using Java/J2EE instead of PHP, or Oracle instead of MySQL, or they’re using Perl/Mason instead of your favorite (PHP) framework, as Marco m entioned earlier —there are so many to choose from that the number is second only to Java.Do you go in and say your experience building a CMS or e-store trumps their experien ce working on a nascent social network? Do you replace all the Java engineers with PHP ones? Do you replace MySQL with Oracle? Do you rewrite the site from scratch using your favorite framework? I’m not saying that in all these instances these architects shouldn’t have made the decisions they did. I am not qualified to answer that since I didn’t work at these places.But what I do know was that in the vast majority of cases, people went in without considering the existing environment. I do know the dynamics of a Facebook.com is different from the dynamics of Gamespot.com or Amazon.com. I do know a social network is different from a CMS or e-store. And all these solutions are very different from ones in enterprise .Like building Fallingwate r without get ting an adequate survey done, every day people make the mistake of not looking before acting. They try to make PHP look like Java with dollar signs, as Luke mentioned earlier . They expected the environ ment to conform to their reality so they can lord over it with “some isolated man-made imposition.”
  • I’ve long since moved from the woods of Western Pennsylvania to the San Francisco Peninsula. I am fortunate that my weekly run passes with a near-constant view of the most recognizable architecture in the American West : References: Image licensed from: http://www.istockphoto.com/file_closeup.php?id=6091847
  • my weekend run
  • What’s interesting is that there are much longer spans in the country and the world. Even in the same city, there exists a beautiful bridge that is both longer and of more utility. And yet this bridge represents the icon of San Francisco and of the state as a whole.
  • The original design was for a hybrid cantilever and suspension structure. But it was replaced with a pure suspension design because the former was deemed too ugly. A pure suspension of this length had never been attempted before.
  • Irving Morrow designed the bridge tower, lighting, and pedestrian walkways with an entirely Art Deco decorative influence.
  • The bridge was painted in a specially formulated anti-rust paint in International Orange on demand from locals. The War Department, and others. The Navy alone originally demanded it be painted black with y ellow stripes to assure visibility with passing ships . The rust-colored p aint evokes the hills of Marin to the north as well as the setting sun. It is natural and visible enough to be safe.
  • rust-colored suspension-only art deco structure It becomes an icon. Every week I pass by it and am inspired and thankful I can live in so beautiful a city.
  • This quote is the origin of “design pattern.”
  • But how are we to know which way to do it? Or even if it is the right pattern to be using in our situation? The answer is found in how both a house in Pennsylvania and a bridge in San Francisco represent ultimate expressions of architecture. They are wholly appropriate for the environment in which they stand.
  • A cantilever is wholly appropriate to create the floating look of Fallingwater, but that same pattern would disrupted the naturalness of the Golden Gate Bridge. When choosing between a singleton and a global variable, which pattern to use is determined by the environment.
  • the first is in Rasmus’s talk and you can see PHP.NET in others.
  • At my previous company, we had the problem where images are stored on the database but need to be generated in multiple sizes and thumbnails and streamed fast to the user. Previews and thumbnails can be dynamically and instantly generated from border parts, primary graphics, colors, and sample font and text and then stored statically on a server using this method. Stuff may be stored in normal form on the database, but representations built dynamically and served statically
  • Address complaint about storing in database…Not all images are stored in the database. In fact, at Tagged, not a single image is stored on a database. However, at Plaxo, the admin tool to manage ecard content requires the assets be done by a non-developer but the only way of pushing out filesystem changes is through change control and there is little need of user generated content. In fact, outside e-cards, the entire Plaxo address book database (including the thumbnail photo of yourself) is stored in a database. I’m not the architect of that, nor do I see much a problem with it.What I point out you CAN now get away with putting image content on the database again with this trick. That’s pretty impressive because different assets on a site (statics, javascripts, web pages, user data, application, back-end, corporate site, customer support, etc) are usually managed on an ad hoc basis optimized for the task at hand. Not all of them can resort to using a filesystem push or web tier restart.
  • Recently, Tagged has run across the very same performance (size and number) issues in Javascript. The solution: Funky caching hooked up to a javascript compressor powered by a Java service back-end to dynamically catenate and compress javascripts into a unique URL on demand.
  • We recently imported 1/64th of our production data over to the staging environment for testing, but the user’s images would take too much time and disk space to import. We could just link the images, but then tester’s didn’t know which ones they uploaded and which ones were proxied from the live website. The solution was to spend an hour writing a Funky Caching Proxy. If the image was missing, the ErrorDocument handler would try to grav the image from the production website and add a watermark .
  • Since this is only for testing, there is no need to waste disk space by storing the created file. The performance hit of real-time generation of redundant requests is unnoticeable to Quality Assurance.
  • With a bit of creativity this concept can apply to modern applications where instead of caching on the filesystem, you cache in memcache; instead of bypassing the application server, you bypass the web servers themselves with a content distribution network ; instead of serving static content from the edge, you serve dynamic pages.Whether to use it and how to use it is always determined by the environment.
  • I hope this tour helps you see software development in a different way—that finding solutions are about using the right solution in a manner that fits with the environment. Even then I realize that we can’t architect structures that work as harmoniously together as a city such as San Francisco. Story of Rounded Rectangles… Bill Atkinson worked mostly at home, but whenever he made significant progress he rushed in to Apple to show it off to anyone who would appreciate it. This time, he visited the Macintosh offices at Texaco Towers to show off his brand new oval routines, which were implemented using a really clever algorithm. Bill had added new code to QuickDraw (which was still called LisaGraf at this point) to draw circles and ovals very quickly. That was a bit hard to do on the Macintosh, since the math for circles usually involved taking square roots, and the 68000 processor in the Lisa and Macintosh didn't support floating point operations. But Bill had come up with a clever way to do the circle calculation that only used addition and subtraction, not even multiplication or division, which the 68000 could do, but was kind of slow at. Bill's technique used the fact the sum of a sequence of odd numbers is always the next perfect square (For example, 1 + 3 = 4, 1 + 3 + 5 = 9, 1 + 3 + 5 + 7 = 16, etc). So he could figure out when to bump the dependent coordinate value by iterating in a loop until a threshold was exceeded. This allowed QuickDraw to draw ovals very quickly. Bill fired up his demo and it quickly filled the Lisa screen with randomly-sized ovals, faster than you thought was possible. But something was bothering Steve Jobs. "Well, circles and ovals are good, but how about drawing rectangles with rounded corners? Can we do that now, too?" "No, there's no way to do that. In fact it would be really hard to do, and I don't think we really need it". I think Bill was a little miffed that Steve wasn't raving over the fast ovals and still wanted more. Steve suddenly got more intense. "Rectangles with rounded corners are everywhere! Just look around this room!". And sure enough, there were lots of them, like the whiteboard and some of the desks and tables. Then he pointed out the window. "And look outside, there's even more, practically everywhere you look!". He even persuaded Bill to take a quick walk around the block with him, pointing out every rectangle with rounded corners that he could find. When Steve and Bill passed a no-parking sign with rounded corners, it did the trick. "OK, I give up", Bill pleaded. "I'll see if it's as hard as I thought." He went back home to work on it. Bill returned to Texaco Towers the following afternoon, with a big smile on his face. His demo was now drawing rectangles with beautifully rounded corners blisteringly fast, almost at the speed of plain rectangles. When he added the code to LisaGraf, he named the new primitive "RoundRects". Over the next few months, roundrects worked their way into various parts of the user interface, and soon became indispensable.
  • Transcript

    • 1. PHP without PHP
      • The Philosophy of Good Architecture
      • terry chay
      • 2010-03-10T09:30+0500
      • Confoo
      • Montreal, Canada
    • 2. Funky Caching
      • Prologue #1
    • 3. aka
      • ErrorDocument trick
      • Smarter Caching
      • … Rasmus’s Trick (Stig’s trick)
      • Go into apache.conf (or .htaccess) and
          • ErrorDocument 404 /error.php
    • 4. Error.PHP
              • $filepath  =  parse_url ( $_SERVER [ 'REQUEST_URI' ],  PHP_URL_PATH );  //or $_SERVER['REDIRECT_URL']
              • $basepath  =  dirname ( __FILE__ ). DIR_SEP ;
              •  
              • // Test to see if you can work with it
              • if ( false ) {  // …EDIT…
              •      //output a 404 page
              •      include( '404.html' );  // see  http://www.alistapart.com/articles/perfect404/  for tips
              •      return;
              • }
              •  
              • // Generate the file
              • //  …EDIT…
              • $data  =  'something' ;
              •  
              • // Don't send 404 back, send 200 OK because this is a pretty smart 404
              • // not a clueless one!  http://www.urbandictionary.com/define.php?term =404
              • header ( sprintf ( '%s 200' ,  $_SERVER [ 'SERVER_PRO TOCOL' ]));
              • //Show the file
              • echo  $data ;
              •  
              • //Store the page to bypass PHP on the next request. Use a temp file with a
              • // link trick in order to avoid race conditions between concurrent PHP
              • // processes.
              • $tmpfile  =  tempnam ( $basepath . 'tmp' , 'fc' );
              • $fp  =  fopen ( $tmpfile , 'w' );  //remove the "_" this is crashing my blog syntax hilighter
              • fputs ( $fp ,  $data );
              • fclose ( $fp );
              • @ link ( $basepath . $filepath ,  $tmpfile );  //suppress errors due to losing race
              • unlink ( $tmpfile );
    • 5. Error.PHP
              • $filepath  =  parse_url ( $_SERVER [ 'REQUEST_URI' ],  PHP_URL_PATH );  //or $_SERVER['REDIRECT_URL']
              • $basepath  =  dirname ( __FILE__ ). DIR_SEP ;
              •  
              • // Test to see if you can work with it
              • if ( false ) {  // …EDIT…
              •      //output a 404 page
              •      include( '404.html' );  // see  http://www.alistapart.com/articles/perfect404/  for tips
              •      return;
              • }
              •  
              • // Generate the file
              • //  …EDIT…
              • $data  =  'something' ;
              •  
              • // Don't send 404 back, send 200 OK because this is a pretty smart 404
              • // not a clueless one!  http://www.urbandictionary.com/define.php?term =404
              • header ( sprintf ( '%s 200' ,  $_SERVER [ 'SERVER_PRO TOCOL' ]));
              • //Show the file
              • echo  $data ;
              •  
              • //Store the page to bypass PHP on the next request. Use a temp file with a
              • // link trick in order to avoid race conditions between concurrent PHP
              • // processes.
              • $tmpfile  =  tempnam ( $basepath . 'tmp' , 'fc' );
              • $fp  =  fopen ( $tmpfile , 'w' );  //remove the "_" this is crashing my blog syntax hilighter
              • fputs ( $fp ,  $data );
              • fclose ( $fp );
              • @ link ( $basepath . $filepath ,  $tmpfile );  //suppress errors due to losing race
              • unlink ( $tmpfile );
      Ceheck
    • 6. Error.PHP
              • $filepath  =  parse_url ( $_SERVER [ 'REQUEST_URI' ],  PHP_URL_PATH );  //or $_SERVER['REDIRECT_URL']
              • $basepath  =  dirname ( __FILE__ ). DIR_SEP ;
              •  
              • // Test to see if you can work with it
              • if ( false ) {  // …EDIT…
              •      //output a 404 page
              •      include( '404.html' );  // see  http://www.alistapart.com/articles/perfect404/  for tips
              •      return;
              • }
              •  
              • // Generate the file
              • //  …EDIT…
              • $data  =  'something' ;
              •  
              • // Don't send 404 back, send 200 OK because this is a pretty smart 404
              • // not a clueless one!  http://www.urbandictionary.com/define.php?term =404
              • header ( sprintf ( '%s 200' ,  $_SERVER [ 'SERVER_PRO TOCOL' ]));
              • //Show the file
              • echo  $data ;
              •  
              • //Store the page to bypass PHP on the next request. Use a temp file with a
              • // link trick in order to avoid race conditions between concurrent PHP
              • // processes.
              • $tmpfile  =  tempnam ( $basepath . 'tmp' , 'fc' );
              • $fp  =  fopen ( $tmpfile , 'w' );  //remove the "_" this is crashing my blog syntax hilighter
              • fputs ( $fp ,  $data );
              • fclose ( $fp );
              • @ link ( $basepath . $filepath ,  $tmpfile );  //suppress errors due to losing race
              • unlink ( $tmpfile );
    • 7. PHP without PHP
    • 8. Paradigms
      • Prologue #2
    • 9. Code Complete
      • The metaphor of Code as construction comes from this book…
      • We now know this is fundamentally wrong…
    • 10. Mythical Man Month
      • “man-month” is a term from construction work
      • The premise is that man and months are interchangeable.
      • This means that in order to reach a deadline I simply add more people to the project.
      reorganize notes too much text
    • 11.
      • First consider something like painting a fence: everything is partionable (man-month).
      • … add a constant time for training.
      • … add communication cost: n(n-1)/2.
      • Compare to the unpartitionable (single man)
      • Adding people to a late project makes it later!
      too much text
    • 12. Engineer and Architect graphics
    • 13. 1 Fallingwater
      • organic, democratic, plasticity, continuity
    • 14.  
    • 15. …including me
    • 16. Hatchway Staircase
      • View at the main (living room) level, from the bridge (from east)
    • 17. Beyond the glass
      • Fallingwater: Living room terraces and glass walls (from east).
    • 18. Fall Foliage
      • View from lookout, downstream.
    • 19. Fall Foliage
      • View from lookout, downstream.
    • 20. No barriers
      • Detail: corner window at the guest house, from southeast.
    • 21. Existing tree
      • The trellis over the driveway is built to accommodate a tree.
    • 22. Local quarry
      • Living room, west (downstream) side, from southeast
    • 23. Existing boulder
      • Living room fireplace and hearth, looking from kitchen door to south windows
    • 24.  
    • 25. Philosophy
      • organic,
      • democratic,
      • plasticity,
      • continuity.
    • 26. Why on PHP?
    • 27. Harmony with Environment
      • Apache web server: ErrorDocument
      • Customer-centric: Performance paramount
      • Relational Database: Slow persistence
      • Harmony with PHP itself…
    • 28. Architecture of PHP
      • Modern web architecture
      write text to talk about it
    • 29. PHP is “Cheap”
      • “A project done in Java will cost 5 times as much, take twice as long, and be harder to maintain than a project done in a scripting language such as PHP or Perl.”
      • —Phillip Greenspun
      graphcis money shorten quote
    • 30. PHP is “Scalable”
      • “ That a Java servlet performs better than a PHP script, under optimal conditions [has] nothing to do with scalability. The point is can your application continue to deliver consistent performance as volume increases. PHP delegates all the ‘hard stuff’ to other systems.”
      • — Harry Fuecks
      graphics notes? shortne quote
    • 31. PHP is “Pragmatic”
      • “ PHP is not about purity in CS principles or architecture; it is about solving the ugly web problem with an admittedly ugly, but extremely functional and convenient solution. If you are looking for purity, you are in the wrong boat. Get out now before you get hit by a wet cat!”
      • — Rasmus Lerdorf
      graphics shorten quote
    • 32. 2 Bellefield Towers
      • Design Hubris
    • 33. map of pit maybe
    • 34. …walking to Bellefield Towers
    • 35. make video of wpaking towards?
    • 36. Gothic Romanesque architecture
      • The current Bellefield Church (two blocks away)
    • 37. Other city towers
      • Allegheny Courthouse Main Tower & Four Tower Types at Jail (and other towers)
    • 38. Tower of Learning
      • Within spitting distance of Bellefield Towers
    • 39. Carnegie Library
      • Most iconic moment in baseball history
      redo builds break out slides quots
    • 40. Why is Bellefield Towers so Ugly?
    • 41. Design Hubris?
      • Frameworks (almost by definition)
      • Develop in the PHP community?
      • Take over an existing project?
      • Limitations of Site Operations? Hosting? or Virtual hosting?
      • Business needs trump programmer desire?
      • Dealt with a user request?
      break out slides, graphics
    • 42. …at Tagged add slideshow
    • 43. …at Tagged
      • Cubes
      • Waterfall process
      • Oracle RAC
      • Java
      • PHP4
      • Zend Accelerator
      break out slides
    • 44. Rework
      • From the Founders of 37 Signals
      • Last Book: “Getting Real”
    • 45. Rework
      • “ Not Just Bad , but dangerous ”
      • —John Vincent, Leon Restaurants
    • 46. Solutions Consider Environment
    • 47. 3 Golden Gate Bridge
      • The Design Pattern
    • 48. design build quote of goldne gate bridge
    • 49. the other bridge quote from emperor norton
    • 50. quotes of sf
    • 51.  
    • 52. Same problem, different pattern
      • Original design was hybrid cantilever-suspension. Replaced by purse suspension
    • 53. Art Deco
      • Bridge Tower, Lighting, pedestrial walkway
      quote of art deco movemebnt
    • 54. International Orange
      • Rust colored like the environment it lives in … and safe.
    • 55. Part of the whole
    • 56. Design Patterns Defined
      • “Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice.”
      shorten quote
    • 57. Certainly iconic
      • Me in front of both icons
    • 58. Never the same way twice
      • How do you know which one?
      • How do you know which way?
      blend transition in stages
    • 59. Funky Caching again
      • “search for the closest matching valid URL and redirect, and use attempted url text as a DB keyword lookup”
      • —Rasmus Lerdorf
    • 60. cleanup text
    • 61.  
    • 62. Javascript and CSS compiling & caching put a graphic or some text
    • 63.  
    • 64.  
    • 65.
      • <?php
      • $watermark  =  '3129080702_c4e76f71d7_o.png' ;
      • $dead_url  =  ' http://example.com/dead_image.png' ;
      •  
      • // {{{ start_image($filename, &$data)
      • /**
      • * Creates a gd handle for a valid file
      • * @param $filename string the file to get
      • * @param $data array the imagesize
      • * @return resource GD handle
      • */
      • function  start_image ( $filename , & $data ) {
      •      $data  = @ getimagesize ( $filename );
      •      if (empty( $data )) { return  null ; }
      •      $data [ 'ratio' ] =  $data [ 0 ]/ $data [ 1 ];
      •      switch( $data [ 2 ]) {
      •          case  IMG_GIF : return  imagecreatefromgif ( $filename );
      •          case  3 :  //problem where IMG_PNG is not bound correctly for my install
      •          case  IMG_PNG : return  imagecreatefrompng ( $filename );
      •          case  IMG_JPG : return  imagecreatefromjpeg ( $filename );
      •          case  IMG_WBMP : return  imagecreatefromwbmp ( $filename );
      •          case  IMG_XPM : return  imagecreatefromxbm ( $filename );
      •      }
      •      return  null ;
      • }   
      • // }}}
      • $requestimg  =  $_SERVER [ 'REDIRECT_URL' ];
      • if (! $_SERVER [ 'QUERY_STRING' ]) {
      •      // redirect user to invalid image
      •      tag_http :: redirect ( $dead_url );
      •      return  '' ;
      • }
      • // grab image to temp {{{
      • $ch  =  curl_init ( $_SERVER [ 'QUERY_STRING' ]);
      • $tempfile  =  tempnam ( '/tmp' ,  'prod_remote_' );
      • $fp  =  f_open ( $tempfile ,  'w' );  //again delete the &quot;_&quot;
      • curl_setopt ( $ch ,  CURLOPT_FILE ,  $fp );
      • curl_setopt ( $ch ,  CURLOPT_HEADER ,  0 );
      • curl_exec_ ( $ch );  //delete the final &quot;_&quot;
      • curl_close ( $ch );
      • fclose ( $fp );
      • // }}}
      • // configure image and dimensions {{{
      • $size_data  = array();
      • $im  =  start_image ( $tempfile ,  $size_data );
      • if (! $im ) {
      •      unlink ( $tempfile );
      •      tag_http :: redirect ( $dead_url );
      •      return;
      • }
      • // }}}
      • // get watermark information {{{
      • $wm_data  = array();
      • $wm  =  start_image ( $watermark ,  $wm_data );
      • if (! $wm ) {
      •      unlink  ( $tempfile );
      •      tag_http :: redirect ( $dead_url );
      •      return;
      • }
      • // }}}
      • // add watermark {{{
      • if ( $size_data [ 'ratio' ]>  $wm_data [ 'ratio' ]) {
      •      // image is wider format than the watermark
      •      $new_smaller_dim  =  $wm_data [ 0 ] * ( $size_data [ 1 ]/ $wm_data [ 1 ]);
      •      $dst_x  = ( $size_data [ 0 ] -  $new_smaller_dim )/ 2 ;
      •      $dst_y  =  0 ;
      •      $dst_w  =  $new_smaller_dim ;
      •      $dst_h  =  $size_data [ 1 ];
      • } else {
      •      // image is taller format than the watermark
      •      $new_smaller_dim  =  $wm_data [ 1 ] * ( $size_data [ 0 ]/ $wm_data [ 0 ]);
      •      $dst_x  =  0 ;
      •      $dst_y  = ( $size_data [ 1 ] -  $new_smaller_dim )/ 2 ;
      •      $dst_w  =  $size_data [ 0 ];
      •      $dst_h  =  $new_smaller_dim ;;
      • }
      • imagecopyresized ( $im ,  $wm ,  $dst_x ,  $dst_y ,  0 ,  0 ,  $dst_w ,  $dst_h ,  $wm_data [ 0 ],  $wm_data [ 1 ]);
      • header ( sprintf ( '%s 200' ,  $_SERVER [ 'SERVER_PROTOCOL' ]));
      • header ( sprintf ( 'Content-type: %s' , $size_data [ 'mime' ]));
      • // }}}
      • switch ( $size_data [ 2 ]) {
      •      case  IMG_GIF :  imagegif ( $im ); break;
      •      case  3 :
      •      case  IMG_PNG :  imagepng ( $im ); break;
      •      case  IMG_JPG :  imagejpeg ( $im ); break;
      •      case  IMG_WBMP :  imagewbmp ( $im ); break;
      •      case  IMG_XPM :  imagexbm ( $im ); break;
      • }
      • imagedestroy ( $wm );
      • imagedestroy ( $im );
      • unlink ( $tempfile );
      • ?>
      expand callouts
    • 66.
      • <?php
      • $watermark  =  '3129080702_c4e76f71d7_o.png' ;
      • $dead_url  =  ' http://example.com/dead_image.png' ;
      •  
      • // {{{ start_image($filename, &$data)
      • /**
      • * Creates a gd handle for a valid file
      • * @param $filename string the file to get
      • * @param $data array the imagesize
      • * @return resource GD handle
      • */
      • function  start_image ( $filename , & $data ) {
      •      $data  = @ getimagesize ( $filename );
      •      if (empty( $data )) { return  null ; }
      •      $data [ 'ratio' ] =  $data [ 0 ]/ $data [ 1 ];
      •      switch( $data [ 2 ]) {
      •          case  IMG_GIF : return  imagecreatefromgif ( $filename );
      •          case  3 :  //problem where IMG_PNG is not bound correctly for my install
      •          case  IMG_PNG : return  imagecreatefrompng ( $filename );
      •          case  IMG_JPG : return  imagecreatefromjpeg ( $filename );
      •          case  IMG_WBMP : return  imagecreatefromwbmp ( $filename );
      •          case  IMG_XPM : return  imagecreatefromxbm ( $filename );
      •      }
      •      return  null ;
      • }   
      • // }}}
      • $requestimg  =  $_SERVER [ 'REDIRECT_URL' ];
      • if (! $_SERVER [ 'QUERY_STRING' ]) {
      •      // redirect user to invalid image
      •      tag_http :: redirect ( $dead_url );
      •      return  '' ;
      • }
      • // grab image to temp {{{
      • $ch  =  curl_init ( $_SERVER [ 'QUERY_STRING' ]);
      • $tempfile  =  tempnam ( '/tmp' ,  'prod_remote_' );
      • $fp  =  f_open ( $tempfile ,  'w' );  //again delete the &quot;_&quot;
      • curl_setopt ( $ch ,  CURLOPT_FILE ,  $fp );
      • curl_setopt ( $ch ,  CURLOPT_HEADER ,  0 );
      • curl_exec_ ( $ch );  //delete the final &quot;_&quot;
      • curl_close ( $ch );
      • fclose ( $fp );
      • // }}}
      • // configure image and dimensions {{{
      • $size_data  = array();
      • $im  =  start_image ( $tempfile ,  $size_data );
      • if (! $im ) {
      •      unlink ( $tempfile );
      •      tag_http :: redirect ( $dead_url );
      •      return;
      • }
      • // }}}
      • // get watermark information {{{
      • $wm_data  = array();
      • $wm  =  start_image ( $watermark ,  $wm_data );
      • if (! $wm ) {
      •      unlink  ( $tempfile );
      •      tag_http :: redirect ( $dead_url );
      •      return;
      • }
      • // }}}
      • // add watermark {{{
      • if ( $size_data [ 'ratio' ]>  $wm_data [ 'ratio' ]) {
      •      // image is wider format than the watermark
      •      $new_smaller_dim  =  $wm_data [ 0 ] * ( $size_data [ 1 ]/ $wm_data [ 1 ]);
      •      $dst_x  = ( $size_data [ 0 ] -  $new_smaller_dim )/ 2 ;
      •      $dst_y  =  0 ;
      •      $dst_w  =  $new_smaller_dim ;
      •      $dst_h  =  $size_data [ 1 ];
      • } else {
      •      // image is taller format than the watermark
      •      $new_smaller_dim  =  $wm_data [ 1 ] * ( $size_data [ 0 ]/ $wm_data [ 0 ]);
      •      $dst_x  =  0 ;
      •      $dst_y  = ( $size_data [ 1 ] -  $new_smaller_dim )/ 2 ;
      •      $dst_w  =  $size_data [ 0 ];
      •      $dst_h  =  $new_smaller_dim ;;
      • }
      • imagecopyresized ( $im ,  $wm ,  $dst_x ,  $dst_y ,  0 ,  0 ,  $dst_w ,  $dst_h ,  $wm_data [ 0 ],  $wm_data [ 1 ]);
      • header ( sprintf ( '%s 200' ,  $_SERVER [ 'SERVER_PROTOCOL' ]));
      • header ( sprintf ( 'Content-type: %s' , $size_data [ 'mime' ]));
      • // }}}
      • switch ( $size_data [ 2 ]) {
      •      case  IMG_GIF :  imagegif ( $im ); break;
      •      case  3 :
      •      case  IMG_PNG :  imagepng ( $im ); break;
      •      case  IMG_JPG :  imagejpeg ( $im ); break;
      •      case  IMG_WBMP :  imagewbmp ( $im ); break;
      •      case  IMG_XPM :  imagexbm ( $im ); break;
      • }
      • imagedestroy ( $wm );
      • imagedestroy ( $im );
      • unlink ( $tempfile );
      • ?>
    • 67.
      • <?php
      • $watermark  =  '3129080702_c4e76f71d7_o.png' ;
      • $dead_url  =  ' http://example.com/dead_image.png' ;
      •  
      • // {{{ start_image($filename, &$data)
      • /**
      • * Creates a gd handle for a valid file
      • * @param $filename string the file to get
      • * @param $data array the imagesize
      • * @return resource GD handle
      • */
      • function  start_image ( $filename , & $data ) {
      •      $data  = @ getimagesize ( $filename );
      •      if (empty( $data )) { return  null ; }
      •      $data [ 'ratio' ] =  $data [ 0 ]/ $data [ 1 ];
      •      switch( $data [ 2 ]) {
      •          case  IMG_GIF : return  imagecreatefromgif ( $filename );
      •          case  3 :  //problem where IMG_PNG is not bound correctly for my install
      •          case  IMG_PNG : return  imagecreatefrompng ( $filename );
      •          case  IMG_JPG : return  imagecreatefromjpeg ( $filename );
      •          case  IMG_WBMP : return  imagecreatefromwbmp ( $filename );
      •          case  IMG_XPM : return  imagecreatefromxbm ( $filename );
      •      }
      •      return  null ;
      • }   
      • // }}}
      • $requestimg  =  $_SERVER [ 'REDIRECT_URL' ];
      • if (! $_SERVER [ 'QUERY_STRING' ]) {
      •      // redirect user to invalid image
      •      tag_http :: redirect ( $dead_url );
      •      return  '' ;
      • }
      • // grab image to temp {{{
      • $ch  =  curl_init ( $_SERVER [ 'QUERY_STRING' ]);
      • $tempfile  =  tempnam ( '/tmp' ,  'prod_remote_' );
      • $fp  =  f_open ( $tempfile ,  'w' );  //again delete the &quot;_&quot;
      • curl_setopt ( $ch ,  CURLOPT_FILE ,  $fp );
      • curl_setopt ( $ch ,  CURLOPT_HEADER ,  0 );
      • curl_exec_ ( $ch );  //delete the final &quot;_&quot;
      • curl_close ( $ch );
      • fclose ( $fp );
      • // }}}
      • // configure image and dimensions {{{
      • $size_data  = array();
      • $im  =  start_image ( $tempfile ,  $size_data );
      • if (! $im ) {
      •      unlink ( $tempfile );
      •      tag_http :: redirect ( $dead_url );
      •      return;
      • }
      • // }}}
      • // get watermark information {{{
      • $wm_data  = array();
      • $wm  =  start_image ( $watermark ,  $wm_data );
      • if (! $wm ) {
      •      unlink  ( $tempfile );
      •      tag_http :: redirect ( $dead_url );
      •      return;
      • }
      • // }}}
      • // add watermark {{{
      • if ( $size_data [ 'ratio' ]>  $wm_data [ 'ratio' ]) {
      •      // image is wider format than the watermark
      •      $new_smaller_dim  =  $wm_data [ 0 ] * ( $size_data [ 1 ]/ $wm_data [ 1 ]);
      •      $dst_x  = ( $size_data [ 0 ] -  $new_smaller_dim )/ 2 ;
      •      $dst_y  =  0 ;
      •      $dst_w  =  $new_smaller_dim ;
      •      $dst_h  =  $size_data [ 1 ];
      • } else {
      •      // image is taller format than the watermark
      •      $new_smaller_dim  =  $wm_data [ 1 ] * ( $size_data [ 0 ]/ $wm_data [ 0 ]);
      •      $dst_x  =  0 ;
      •      $dst_y  = ( $size_data [ 1 ] -  $new_smaller_dim )/ 2 ;
      •      $dst_w  =  $size_data [ 0 ];
      •      $dst_h  =  $new_smaller_dim ;;
      • }
      • imagecopyresized ( $im ,  $wm ,  $dst_x ,  $dst_y ,  0 ,  0 ,  $dst_w ,  $dst_h ,  $wm_data [ 0 ],  $wm_data [ 1 ]);
      • header ( sprintf ( '%s 200' ,  $_SERVER [ 'SERVER_PROTOCOL' ]));
      • header ( sprintf ( 'Content-type: %s' , $size_data [ 'mime' ]));
      • // }}}
      • switch ( $size_data [ 2 ]) {
      •      case  IMG_GIF :  imagegif ( $im ); break;
      •      case  3 :
      •      case  IMG_PNG :  imagepng ( $im ); break;
      •      case  IMG_JPG :  imagejpeg ( $im ); break;
      •      case  IMG_WBMP :  imagewbmp ( $im ); break;
      •      case  IMG_XPM :  imagexbm ( $im ); break;
      • }
      • imagedestroy ( $wm );
      • imagedestroy ( $im );
      • unlink ( $tempfile );
      • ?>
    • 68.
        • <?php
      • $watermark  =  '3129080702_c4e76f71d7_o.png' ;
      • $dead_url  =  ' http://example.com/dead_image.png' ;
      •  
      • // {{{ start_image($filename, &$data)
      • /**
      • * Creates a gd handle for a valid file
      • * @param $filename string the file to get
      • * @param $data array the imagesize
      • * @return resource GD handle
      • */
      • function  start_image ( $filename , & $data ) {
      •      $data  = @ getimagesize ( $filename );
      •      if (empty( $data )) { return  null ; }
      •      $data [ 'ratio' ] =  $data [ 0 ]/ $data [ 1 ];
      •      switch( $data [ 2 ]) {
      •          case  IMG_GIF : return  imagecreatefromgif ( $filename );
      •          case  3 :  //problem where IMG_PNG is not bound correctly for my install
      •          case  IMG_PNG : return  imagecreatefrompng ( $filename );
      •          case  IMG_JPG : return  imagecreatefromjpeg ( $filename );
      •          case  IMG_WBMP : return  imagecreatefromwbmp ( $filename );
      •          case  IMG_XPM : return  imagecreatefromxbm ( $filename );
      •      }
      •      return  null ;
      • }   
      • // }}}
      • $requestimg  =  $_SERVER [ 'REDIRECT_URL' ];
      • if (! $_SERVER [ 'QUERY_STRING' ]) {
      •      // redirect user to invalid image
      •      tag_http :: redirect ( $dead_url );
      •      return  '' ;
      • }
      • // grab image to temp {{{
      • $ch  =  curl_init ( $_SERVER [ 'QUERY_STRING' ]);
      • $tempfile  =  tempnam ( '/tmp' ,  'prod_remote_' );
      • $fp  =  f_open ( $tempfile ,  'w' );  //again delete the &quot;_&quot;
      • curl_setopt ( $ch ,  CURLOPT_FILE ,  $fp );
      • curl_setopt ( $ch ,  CURLOPT_HEADER ,  0 );
      • curl_exec_ ( $ch );  //delete the final &quot;_&quot;
      • curl_close ( $ch );
      • fclose ( $fp );
      • // }}}
      • // configure image and dimensions {{{
      • $size_data  = array();
      • $im  =  start_image ( $tempfile ,  $size_data );
      • if (! $im ) {
      •      unlink ( $tempfile );
      •      tag_http :: redirect ( $dead_url );
      •      return;
      • }
      • // }}}
      • // get watermark information {{{
      • $wm_data  = array();
      • $wm  =  start_image ( $watermark ,  $wm_data );
      • if (! $wm ) {
      •      unlink  ( $tempfile );
      •      tag_http :: redirect ( $dead_url );
      •      return;
      • }
      • // }}}
      • // add watermark {{{
      • if ( $size_data [ 'ratio' ]>  $wm_data [ 'ratio' ]) {
      •      // image is wider format than the watermark
      •      $new_smaller_dim  =  $wm_data [ 0 ] * ( $size_data [ 1 ]/ $wm_data [ 1 ]);
      •      $dst_x  = ( $size_data [ 0 ] -  $new_smaller_dim )/ 2 ;
      •      $dst_y  =  0 ;
      •      $dst_w  =  $new_smaller_dim ;
      •      $dst_h  =  $size_data [ 1 ];
      • } else {
      •      // image is taller format than the watermark
      •      $new_smaller_dim  =  $wm_data [ 1 ] * ( $size_data [ 0 ]/ $wm_data [ 0 ]);
      •      $dst_x  =  0 ;
      •      $dst_y  = ( $size_data [ 1 ] -  $new_smaller_dim )/ 2 ;
      •      $dst_w  =  $size_data [ 0 ];
      •      $dst_h  =  $new_smaller_dim ;;
      • }
      • imagecopyresized ( $im ,  $wm ,  $dst_x ,  $dst_y ,  0 ,  0 ,  $dst_w ,  $dst_h ,  $wm_data [ 0 ],  $wm_data [ 1 ]);
      • header ( sprintf ( '%s 200' ,  $_SERVER [ 'SERVER_PROTOCOL' ]));
      • header ( sprintf ( 'Content-type: %s' , $size_data [ 'mime' ]));
      • // }}}
      • switch ( $size_data [ 2 ]) {
      •      case  IMG_GIF :  imagegif ( $im ); break;
      •      case  3 :
      •      case  IMG_PNG :  imagepng ( $im ); break;
      •      case  IMG_JPG :  imagejpeg ( $im ); break;
      •      case  IMG_WBMP :  imagewbmp ( $im ); break;
      •      case  IMG_XPM :  imagexbm ( $im ); break;
      • }
      • imagedestroy ( $wm );
      • imagedestroy ( $im );
      • unlink ( $tempfile );
      • ?>
    • 69.
        • <?php
      • $watermark  =  '3129080702_c4e76f71d7_o.png' ;
      • $dead_url  =  ' http://example.com/dead_image.png' ;
      •  
      • // {{{ start_image($filename, &$data)
      • /**
      • * Creates a gd handle for a valid file
      • * @param $filename string the file to get
      • * @param $data array the imagesize
      • * @return resource GD handle
      • */
      • function  start_image ( $filename , & $data ) {
      •      $data  = @ getimagesize ( $filename );
      •      if (empty( $data )) { return  null ; }
      •      $data [ 'ratio' ] =  $data [ 0 ]/ $data [ 1 ];
      •      switch( $data [ 2 ]) {
      •          case  IMG_GIF : return  imagecreatefromgif ( $filename );
      •          case  3 :  //problem where IMG_PNG is not bound correctly for my install
      •          case  IMG_PNG : return  imagecreatefrompng ( $filename );
      •          case  IMG_JPG : return  imagecreatefromjpeg ( $filename );
      •          case  IMG_WBMP : return  imagecreatefromwbmp ( $filename );
      •          case  IMG_XPM : return  imagecreatefromxbm ( $filename );
      •      }
      •      return  null ;
      • }   
      • // }}}
      • $requestimg  =  $_SERVER [ 'REDIRECT_URL' ];
      • if (! $_SERVER [ 'QUERY_STRING' ]) {
      •      // redirect user to invalid image
      •      tag_http :: redirect ( $dead_url );
      •      return  '' ;
      • }
      • // grab image to temp {{{
      • $ch  =  curl_init ( $_SERVER [ 'QUERY_STRING' ]);
      • $tempfile  =  tempnam ( '/tmp' ,  'prod_remote_' );
      • $fp  =  f_open ( $tempfile ,  'w' );  //again delete the &quot;_&quot;
      • curl_setopt ( $ch ,  CURLOPT_FILE ,  $fp );
      • curl_setopt ( $ch ,  CURLOPT_HEADER ,  0 );
      • curl_exec_ ( $ch );  //delete the final &quot;_&quot;
      • curl_close ( $ch );
      • fclose ( $fp );
      • // }}}
      • // configure image and dimensions {{{
      • $size_data  = array();
      • $im  =  start_image ( $tempfile ,  $size_data );
      • if (! $im ) {
      •      unlink ( $tempfile );
      •      tag_http :: redirect ( $dead_url );
      •      return;
      • }
      • // }}}
      • // get watermark information {{{
      • $wm_data  = array();
      • $wm  =  start_image ( $watermark ,  $wm_data );
      • if (! $wm ) {
      •      unlink  ( $tempfile );
      •      tag_http :: redirect ( $dead_url );
      •      return;
      • }
      • // }}}
      • // add watermark {{{
      • if ( $size_data [ 'ratio' ]>  $wm_data [ 'ratio' ]) {
      •      // image is wider format than the watermark
      •      $new_smaller_dim  =  $wm_data [ 0 ] * ( $size_data [ 1 ]/ $wm_data [ 1 ]);
      •      $dst_x  = ( $size_data [ 0 ] -  $new_smaller_dim )/ 2 ;
      •      $dst_y  =  0 ;
      •      $dst_w  =  $new_smaller_dim ;
      •      $dst_h  =  $size_data [ 1 ];
      • } else {
      •      // image is taller format than the watermark
      •      $new_smaller_dim  =  $wm_data [ 1 ] * ( $size_data [ 0 ]/ $wm_data [ 0 ]);
      •      $dst_x  =  0 ;
      •      $dst_y  = ( $size_data [ 1 ] -  $new_smaller_dim )/ 2 ;
      •      $dst_w  =  $size_data [ 0 ];
      •      $dst_h  =  $new_smaller_dim ;;
      • }
      • imagecopyresized ( $im ,  $wm ,  $dst_x ,  $dst_y ,  0 ,  0 ,  $dst_w ,  $dst_h ,  $wm_data [ 0 ],  $wm_data [ 1 ]);
      • header ( sprintf ( '%s 200' ,  $_SERVER [ 'SERVER_PROTOCOL' ]));
      • header ( sprintf ( 'Content-type: %s' , $size_data [ 'mime' ]));
      • // }}}
      • switch ( $size_data [ 2 ]) {
      •      case  IMG_GIF :  imagegif ( $im ); break;
      •      case  3 : case  IMG_PNG :  imagepng ( $im ); break;
      •      case  IMG_JPG :  imagejpeg ( $im ); break;
      •      case  IMG_WBMP :  imagewbmp ( $im ); break;
      •      case  IMG_XPM :  imagexbm ( $im ); break;
      • }
      • imagedestroy ( $wm );
      • imagedestroy ( $im );
      • unlink ( $tempfile );
      • ?>
    • 70.
      • <?php
      • $watermark  =  '3129080702_c4e76f71d7_o.png' ;
      • $dead_url  =  ' http://example.com/dead_image.png' ;
      •  
      • // {{{ start_image($filename, &$data)
      • /**
      • * Creates a gd handle for a valid file
      • * @param $filename string the file to get
      • * @param $data array the imagesize
      • * @return resource GD handle
      • */
      • function  start_image ( $filename , & $data ) {
      •      $data  = @ getimagesize ( $filename );
      •      if (empty( $data )) { return  null ; }
      •      $data [ 'ratio' ] =  $data [ 0 ]/ $data [ 1 ];
      •      switch( $data [ 2 ]) {
      •          case  IMG_GIF : return  imagecreatefromgif ( $filename );
      •          case  3 :  //problem where IMG_PNG is not bound correctly for my install
      •          case  IMG_PNG : return  imagecreatefrompng ( $filename );
      •          case  IMG_JPG : return  imagecreatefromjpeg ( $filename );
      •          case  IMG_WBMP : return  imagecreatefromwbmp ( $filename );
      •          case  IMG_XPM : return  imagecreatefromxbm ( $filename );
      •      }
      •      return  null ;
      • }   
      • // }}}
      • $requestimg  =  $_SERVER [ 'REDIRECT_URL' ];
      • if (! $_SERVER [ 'QUERY_STRING' ]) {
      •      // redirect user to invalid image
      •      tag_http :: redirect ( $dead_url );
      •      return  '' ;
      • }
      • // grab image to temp {{{
      • $ch  =  curl_init ( $_SERVER [ 'QUERY_STRING' ]);
      • $tempfile  =  tempnam ( '/tmp' ,  'prod_remote_' );
      • $fp  =  f_open ( $tempfile ,  'w' );  //again delete the &quot;_&quot;
      • curl_setopt ( $ch ,  CURLOPT_FILE ,  $fp );
      • curl_setopt ( $ch ,  CURLOPT_HEADER ,  0 );
      • curl_exec_ ( $ch );  //delete the final &quot;_&quot;
      • curl_close ( $ch );
      • fclose ( $fp );
      • // }}}
      • // configure image and dimensions {{{
      • $size_data  = array();
      • $im  =  start_image ( $tempfile ,  $size_data );
      • if (! $im ) {
      •      unlink ( $tempfile );
      •      tag_http :: redirect ( $dead_url );
      •      return;
      • }
      • // }}}
      • // get watermark information {{{
      • $wm_data  = array();
      • $wm  =  start_image ( $watermark ,  $wm_data );
      • if (! $wm ) {
      •      unlink  ( $tempfile );
      •      tag_http :: redirect ( $dead_url );
      •      return;
      • }
      • // }}}
      • // add watermark {{{
      • if ( $size_data [ 'ratio' ]>  $wm_data [ 'ratio' ]) {
      •      // image is wider format than the watermark
      •      $new_smaller_dim  =  $wm_data [ 0 ] * ( $size_data [ 1 ]/ $wm_data [ 1 ]);
      •      $dst_x  = ( $size_data [ 0 ] -  $new_smaller_dim )/ 2 ;
      •      $dst_y  =  0 ;
      •      $dst_w  =  $new_smaller_dim ;
      •      $dst_h  =  $size_data [ 1 ];
      • } else {
      •      // image is taller format than the watermark
      •      $new_smaller_dim  =  $wm_data [ 1 ] * ( $size_data [ 0 ]/ $wm_data [ 0 ]);
      •      $dst_x  =  0 ;
      •      $dst_y  = ( $size_data [ 1 ] -  $new_smaller_dim )/ 2 ;
      •      $dst_w  =  $size_data [ 0 ];
      •      $dst_h  =  $new_smaller_dim ;;
      • }
      • imagecopyresized ( $im ,  $wm ,  $dst_x ,  $dst_y ,  0 ,  0 ,  $dst_w ,  $dst_h ,  $wm_data [ 0 ],  $wm_data [ 1 ]);
      • header ( sprintf ( '%s 200' ,  $_SERVER [ 'SERVER_PROTOCOL' ]));
      • header ( sprintf ( 'Content-type: %s' , $size_data [ 'mime' ]));
      • // }}}
      • switch ( $size_data [ 2 ]) {
      •      case  IMG_GIF :  imagegif ( $im ); break;
      •      case  3 : case  IMG_PNG :  imagepng ( $im ); break;
      •      case  IMG_JPG :  imagejpeg ( $im ); break;
      •      case  IMG_WBMP :  imagewbmp ( $im ); break;
      •      case  IMG_XPM :  imagexbm ( $im ); break;
      • }
      • imagedestroy ( $wm );
      • imagedestroy ( $im );
      • unlink ( $tempfile );
      • ?>
    • 71.
      • <?php
      • $watermark  =  '3129080702_c4e76f71d7_o.png' ;
      • $dead_url  =  ' http://example.com/dead_image.png' ;
      •  
      • // {{{ start_image($filename, &$data)
      • /**
      • * Creates a gd handle for a valid file
      • * @param $filename string the file to get
      • * @param $data array the imagesize
      • * @return resource GD handle
      • */
      • function  start_image ( $filename , & $data ) {
      •      $data  = @ getimagesize ( $filename );
      •      if (empty( $data )) { return  null ; }
      •      $data [ 'ratio' ] =  $data [ 0 ]/ $data [ 1 ];
      •      switch( $data [ 2 ]) {
      •          case  IMG_GIF : return  imagecreatefromgif ( $filename );
      •          case  3 :  //problem where IMG_PNG is not bound correctly for my install
      •          case  IMG_PNG : return  imagecreatefrompng ( $filename );
      •          case  IMG_JPG : return  imagecreatefromjpeg ( $filename );
      •          case  IMG_WBMP : return  imagecreatefromwbmp ( $filename );
      •          case  IMG_XPM : return  imagecreatefromxbm ( $filename );
      •      }
      •      return  null ;
      • }   
      • // }}}
      • $requestimg  =  $_SERVER [ 'REDIRECT_URL' ];
      • if (! $_SERVER [ 'QUERY_STRING' ]) {
      •      // redirect user to invalid image
      •      tag_http :: redirect ( $dead_url );
      •      return  '' ;
      • }
      • // grab image to temp {{{
      • $ch  =  curl_init ( $_SERVER [ 'QUERY_STRING' ]);
      • $tempfile  =  tempnam ( '/tmp' ,  'prod_remote_' );
      • $fp  =  f_open ( $tempfile ,  'w' );  //again delete the &quot;_&quot;
      • curl_setopt ( $ch ,  CURLOPT_FILE ,  $fp );
      • curl_setopt ( $ch ,  CURLOPT_HEADER ,  0 );
      • curl_exec_ ( $ch );  //delete the final &quot;_&quot;
      • curl_close ( $ch );
      • fclose ( $fp );
      • // }}}
      • // configure image and dimensions {{{
      • $size_data  = array();
      • $im  =  start_image ( $tempfile ,  $size_data );
      • if (! $im ) {
      •      unlink ( $tempfile );
      •      tag_http :: redirect ( $dead_url );
      •      return;
      • }
      • // }}}
      • // get watermark information {{{
      • $wm_data  = array();
      • $wm  =  start_image ( $watermark ,  $wm_data );
      • if (! $wm ) {
      •      unlink  ( $tempfile );
      •      tag_http :: redirect ( $dead_url );
      •      return;
      • }
      • // }}}
      • // add watermark {{{
      • if ( $size_data [ 'ratio' ]>  $wm_data [ 'ratio' ]) {
      •      // image is wider format than the watermark
      •      $new_smaller_dim  =  $wm_data [ 0 ] * ( $size_data [ 1 ]/ $wm_data [ 1 ]);
      •      $dst_x  = ( $size_data [ 0 ] -  $new_smaller_dim )/ 2 ;
      •      $dst_y  =  0 ;
      •      $dst_w  =  $new_smaller_dim ;
      •      $dst_h  =  $size_data [ 1 ];
      • } else {
      •      // image is taller format than the watermark
      •      $new_smaller_dim  =  $wm_data [ 1 ] * ( $size_data [ 0 ]/ $wm_data [ 0 ]);
      •      $dst_x  =  0 ;
      •      $dst_y  = ( $size_data [ 1 ] -  $new_smaller_dim )/ 2 ;
      •      $dst_w  =  $size_data [ 0 ];
      •      $dst_h  =  $new_smaller_dim ;;
      • }
      • imagecopyresized ( $im ,  $wm ,  $dst_x ,  $dst_y ,  0 ,  0 ,  $dst_w ,  $dst_h ,  $wm_data [ 0 ],  $wm_data [ 1 ]);
      • header ( sprintf ( '%s 200' ,  $_SERVER [ 'SERVER_PROTOCOL' ]));
      • header ( sprintf ( 'Content-type: %s' , $size_data [ 'mime' ]));
      • // }}}
      • switch ( $size_data [ 2 ]) {
      •      case  IMG_GIF :  imagegif ( $im ); break;
      •      case  3 : case  IMG_PNG :  imagepng ( $im ); break;
      •      case  IMG_JPG :  imagejpeg ( $im ); break;
      •      case  IMG_WBMP :  imagewbmp ( $im ); break;
      •      case  IMG_XPM :  imagexbm ( $im ); break;
      • }
      • imagedestroy ( $wm );
      • imagedestroy ( $im );
      • unlink ( $tempfile );
      • ?>
    • 72. Beyond Funky Caching
    • 73. San Francisco
      • Looking around you for inspiration
      quotes of san francisco?
    • 74. Thanks! [email_address] terrychay.com/blog terrychay.com/blog terrychay.com/blog resize
    • 75. Story about rounded rectangles

    ×