{"id":3768,"date":"2009-09-04T08:14:14","date_gmt":"2009-09-04T15:14:14","guid":{"rendered":"http:\/\/css-tricks.com\/?page_id=3768"},"modified":"2009-09-04T08:14:14","modified_gmt":"2009-09-04T15:14:14","slug":"crop-image","status":"publish","type":"page","link":"https:\/\/css-tricks.com\/snippets\/php\/crop-image\/","title":{"rendered":"Crop Image"},"content":{"rendered":"

Displays a selected chunk of an image. In the example provided, the upper left 100px x 100px are shown.<\/p>\n

<?php\r\n\r\n$filename= \"test.jpg\";\r\nlist($w, $h, $type, $attr) = getimagesize($filename);\r\n$src_im = imagecreatefromjpeg($filename);\r\n\r\n$src_x = '0';   \/\/ begin x\r\n$src_y = '0';   \/\/ begin y\r\n$src_w = '100'; \/\/ width\r\n$src_h = '100'; \/\/ height\r\n$dst_x = '0';   \/\/ destination x\r\n$dst_y = '0';   \/\/ destination y\r\n\r\n$dst_im = imagecreatetruecolor($src_w, $src_h);\r\n$white = imagecolorallocate($dst_im, 255, 255, 255);\r\nimagefill($dst_im, 0, 0, $white);\r\n\r\nimagecopy($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h);\r\n\r\nheader(\"Content-type: image\/png\");\r\nimagepng($dst_im);\r\nimagedestroy($dst_im);\r\n\r\n?><\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"

Displays a selected chunk of an image. In the example provided, the upper left 100px x 100px are shown. <?php $filename= “test.jpg”; list($w, $h, $type, $attr) = getimagesize($filename); $src_im = imagecreatefromjpeg($filename); $src_x = ‘0’; \/\/ begin x $src_y = ‘0’; \/\/ begin y $src_w = ‘100’; \/\/ width $src_h = ‘100’; \/\/ height $dst_x = […]<\/p>\n","protected":false},"author":3,"featured_media":0,"parent":3233,"menu_order":0,"comment_status":"open","ping_status":"closed","template":"page-snippet.php","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"sig_custom_text":"","sig_image_type":"featured-image","sig_custom_image":0,"sig_is_disabled":false,"inline_featured_image":false,"c2c_always_allow_admin_comments":false,"footnotes":""},"tags":[],"acf":[],"jetpack-related-posts":[{"id":323048,"url":"https:\/\/css-tricks.com\/almanac\/properties\/m\/mask-position\/","url_meta":{"origin":3768,"position":0},"title":"mask-position","date":"October 12, 2020","format":false,"excerpt":"In CSS, the mask-position property specifies the initial position of a mask layer image relative to the mask position area. It works like the background-position property. .element { \u00a0 mask-image: url(\"star.svg\"); \u00a0 mask-position: 20px center; } Masking allows you to display selected parts of an element while hiding the rest.\u2026","rel":"","context":"Similar post","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":196146,"url":"https:\/\/css-tricks.com\/almanac\/properties\/b\/background-position\/","url_meta":{"origin":3768,"position":1},"title":"background-position","date":"February 17, 2015","format":false,"excerpt":"The background-position property in CSS allows you to move a background image (or gradient) around within its container. html { background-position: 100px 5px; } It has three different types of values: Length values (e.g. 100px 5px)Percentages (e.g. 100% 5%)Keywords (e.g. top right) The default values are 0 0. This places\u2026","rel":"","context":"In \"background\"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":3730,"url":"https:\/\/css-tricks.com\/snippets\/css\/png-hack-for-ie-6\/","url_meta":{"origin":3768,"position":2},"title":"PNG Hack\/Fix for IE 6","date":"September 4, 2009","format":false,"excerpt":"For CSS background-images .yourselector { width:200px; height:100px; background: url(\/folder\/yourimage.png) no-repeat; _background:none; _filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='\/folder\/yourimage.png',sizingMethod='crop'); } Cannot be used with repeating, needs fixed with and height. For inline HTML images img, .png { position: relative; behavior: expression((this.runtimeStyle.behavior=\"none\")&&(this.pngSet?this.pngSet=true:(this.nodeName == \"IMG\" && this.src.toLowerCase().indexOf('.png')>-1?(this.runtimeStyle.backgroundImage = \"none\", this.runtimeStyle.filter = \"progid:DXImageTransform.Microsoft.AlphaImageLoader(src='\" + this.src + \"', sizingMethod='image')\", this.src =\u2026","rel":"","context":"In "Article"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":203844,"url":"https:\/\/css-tricks.com\/almanac\/properties\/s\/shape-outside\/","url_meta":{"origin":3768,"position":3},"title":"shape-outside","date":"June 17, 2015","format":false,"excerpt":"The shape-outside property controls how content will wrap around a floated element\u2019s bounding-box. Typically this is so that text can reflow over a shape such as a circle, ellipse or a polygon: .element { float: left; shape-outside: circle(50%); width: 200px; height: 200px; } It\u2019s important to note that this property\u2026","rel":"","context":"In \"basic shapes\"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":343405,"url":"https:\/\/css-tricks.com\/almanac\/properties\/m\/mask\/","url_meta":{"origin":3768,"position":4},"title":"mask","date":"July 2, 2021","format":false,"excerpt":"The mask property is a shorthand to specify all mask-* properties. mask can hide part of the element is applied to and It accepts one or more comma-separated values, where each value corresponds to a mask layer. .element { mask: url(mask.png) right bottom \/ 100px repeat-y; } mask is included\u2026","rel":"","context":"Similar post","img":{"alt_text":"","src":"https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2019\/11\/drama-masks.png?fit=1200%2C600&ssl=1&resize=350%2C200","width":350,"height":200},"classes":[]},{"id":372340,"url":"https:\/\/css-tricks.com\/almanac\/properties\/g\/grid\/","url_meta":{"origin":3768,"position":5},"title":"grid","date":"August 8, 2022","format":false,"excerpt":"The grid CSS property is a shorthand that allows you to set all the implicit and the explicit grid properties in a single declaration. .grid-container { display: grid; grid: auto-flow dense \/ repeat(5, 150px); } The above example sets grid-template-columns to repeat(5, 150px) and grid-auto-flow to row dense which creates\u2026","rel":"","context":"Similar post","img":{"alt_text":"","src":"https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2019\/12\/css-tricks-logo-gradient-outline.png?fit=1200%2C600&ssl=1&resize=350%2C200","width":350,"height":200},"classes":[]}],"_links":{"self":[{"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/pages\/3768"}],"collection":[{"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/comments?post=3768"}],"version-history":[{"count":1,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/pages\/3768\/revisions"}],"predecessor-version":[{"id":3769,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/pages\/3768\/revisions\/3769"}],"up":[{"embeddable":true,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/pages\/3233"}],"wp:attachment":[{"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/media?parent=3768"}],"wp:term":[{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/tags?post=3768"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}