{"id":658,"date":"2023-10-06T17:43:33","date_gmt":"2023-10-06T17:43:33","guid":{"rendered":"https:\/\/www.josejavierfm.es\/blog\/?p=658"},"modified":"2023-10-06T18:03:53","modified_gmt":"2023-10-06T18:03:53","slug":"reducir-todas-las-imagenes-de-un-directorio-a-la-mitad-si-son-mayores-de-999px","status":"publish","type":"post","link":"https:\/\/www.josejavierfm.es\/blog\/2023\/10\/06\/reducir-todas-las-imagenes-de-un-directorio-a-la-mitad-si-son-mayores-de-999px\/","title":{"rendered":"reducir todas las imagenes de un directorio a la mitad si son mayores de 999px"},"content":{"rendered":"<p>si tenemos un directorio que contiene im\u00e1genes y queremos reducir su tama\u00f1o, podemos utilizar un script desde un cron que recorre todas las im\u00e1genes, comprueba su tama\u00f1o y si es mayor del esperado lo reduce a la mitad<\/p>\n<pre>\r\n&lt;?php\r\n\r\n\r\n\r\nfunction createthumb($name,$filename,$new_w,$new_h){\r\n\t$system=explode('.',$name);\r\n\t$numparam=count($system);\r\n\t$tipo=\"\";\r\n\tif (preg_match('\/jpg|jpeg\/',$system[$numparam-1])){\r\n\t\t$src_img=imagecreatefromjpeg($name);\r\n\t\t$tipo=\"jpg\";\r\n\t}\r\n\tif (preg_match('\/png\/',$system[$numparam-1])){\r\n\t\t$src_img=imagecreatefrompng($name);\r\n\t\t$tipo=\"png\";\r\n\t}\r\n\tif ($tipo!=\"\"){\r\n\t\t$old_x=imageSX($src_img);\r\n\t\t$old_y=imageSY($src_img);\r\n\t\tif ($old_x > $old_y) {\r\n\t\t\t$thumb_w=$new_w;\r\n\t\t\t$thumb_h=$old_y*($new_h\/$old_x);\r\n\t\t}\r\n\t\tif ($old_x < $old_y) {\r\n\t\t\t$thumb_w=$old_x*($new_w\/$old_y);\r\n\t\t\t$thumb_h=$new_h;\r\n\t\t}\r\n\t\tif ($old_x == $old_y) {\r\n\t\t\t$thumb_w=$new_w;\r\n\t\t\t$thumb_h=$new_h;\r\n\t\t}\r\n\t\t$dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);\r\n\t\t\t\r\n\t\tif ($tipo==\"png\")\r\n\t\t{\r\n\t\t\timagealphablending($dst_img, false);\r\n\t\t\timagesavealpha($dst_img, true);\r\n\t\t\timagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y); \r\n\t\t\timagepng($dst_img,$filename); \r\n\t\t} else {\r\n\t\t\timagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y); \r\n\t\t\timagejpeg($dst_img,$filename); \r\n\t\t}\r\n\t\timagedestroy($dst_img); \r\n\t\timagedestroy($src_img); \r\n\t\treturn $filename;\r\n\t}else{\r\n\t\treturn null;\r\n\t}\r\n}\r\n\r\n\r\n\r\n$directorio = '.';\r\n$ficheros1  = scandir($directorio);\r\n\r\n$imagenes=0;\r\n$contador=0;\r\nforeach ($ficheros1 as $key => $value){\r\n      if (!in_array($value,array(\".\",\"..\"))){\r\n         if (is_dir($directorio . DIRECTORY_SEPARATOR . $value)){\r\n           \/\/no hacer nada\r\n         }else{\r\n            $imagenes++;\r\n            $gis=@getimagesize($value);\r\n            list($width, $height, $type, $attr) = $gis;\r\n            \r\n            if ($width>=1000 || $height>=1000){\r\n                print_r($value);\r\n                echo \" \".$width.\"x\".$height;\r\n               echo \" hay que redimensionar\";\r\n               $filename=\"\".$value;\r\n               $new_w=(int)($width\/2);\r\n               $new_h=(int)($height\/2);\r\n               createthumb($value,$filename,$new_w,$new_h);\r\n               $contador++;\r\n               echo \"<br>\";\r\n            }\r\n\t    \/\/ esto solo si hay problemas de timeout\r\n            \/\/if ($contador==500){break;}\r\n         } \r\n      }\r\n   }\r\n   echo \"Se han convertido $contador de $imagenes imagenes\";\r\n   ?>\r\n<\/pre>\n<p>Se podria mejorar buscando imagenes en lugar de recorrer todos los archivos, pero queda para otro momento<\/p>\n\n<div class=\"twitter-share\"><a href=\"https:\/\/twitter.com\/intent\/tweet?via=josejavierfm\" class=\"twitter-share-button\">Twittear<\/a><\/div>\n","protected":false},"excerpt":{"rendered":"<p>si tenemos un directorio que contiene im\u00e1genes y queremos reducir su tama\u00f1o, podemos utilizar un script desde un cron que recorre todas las im\u00e1genes, comprueba su tama\u00f1o y si es mayor del esperado lo reduce a la mitad &lt;?php function createthumb($name,$filename,$new_w,$new_h){ $system=explode(&#8216;.&#8217;,$name); $numparam=count($system); $tipo=\u00bb\u00bb; if (preg_match(&#8216;\/jpg|jpeg\/&#8217;,$system[$numparam-1])){ $src_img=imagecreatefromjpeg($name); $tipo=\u00bbjpg\u00bb; } if (preg_match(&#8216;\/png\/&#8217;,$system[$numparam-1])){ $src_img=imagecreatefrompng($name); $tipo=\u00bbpng\u00bb; } if [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[15],"tags":[],"_links":{"self":[{"href":"https:\/\/www.josejavierfm.es\/blog\/wp-json\/wp\/v2\/posts\/658"}],"collection":[{"href":"https:\/\/www.josejavierfm.es\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.josejavierfm.es\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.josejavierfm.es\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.josejavierfm.es\/blog\/wp-json\/wp\/v2\/comments?post=658"}],"version-history":[{"count":2,"href":"https:\/\/www.josejavierfm.es\/blog\/wp-json\/wp\/v2\/posts\/658\/revisions"}],"predecessor-version":[{"id":660,"href":"https:\/\/www.josejavierfm.es\/blog\/wp-json\/wp\/v2\/posts\/658\/revisions\/660"}],"wp:attachment":[{"href":"https:\/\/www.josejavierfm.es\/blog\/wp-json\/wp\/v2\/media?parent=658"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.josejavierfm.es\/blog\/wp-json\/wp\/v2\/categories?post=658"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.josejavierfm.es\/blog\/wp-json\/wp\/v2\/tags?post=658"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}