set image width with php? -
I am creating a custom blog with PHP when the user is uploading an article, I have problems with images in the post The width of some images is larger than the main div in my blog (740). I want to use php to check the width of the images, if it is larger than 740, then resize the image to 740.
& lt ;? Php $ dom = new domDocument; $ Dom- & gt; LoadHTML ($ article_content); $ Dom- & gt; Protected WhiteSpace = false; $ Imgs = $ dom- & gt; GetElementsByTagName ("IMG"); $ Links = array (); ($ I = 0; $ i & lt; $ imgs- & gt; Length; $ i ++) {$ links [] = $ imgs- & gt; Items ($ i) - & gt; GetAttribute ("width"); $ Image_path = $ links []; $ Article_source = imagecreatefromstring (file_get_contents ($ image_path)); $ Image_width = imagesx ($ image_source); If ($ image_width> 740) {$ image_width = 740;}}? & Gt; So far, I have a code I do not believe how to set the image width. (The image already has its original width) Update: I am not trying to save or copy the image. I am trying to get to DOM by PHP and trying to set the image width to $ image_width (all images)
Without saving / copying the image, you will need to change the IMG tag in the HTML document with the width attribute.
$ dom = new domDocument; $ Dom- & gt; LoadHTML ($ article_content); $ IGIMENTS = $ DOM- & gt; GetElementsByTagName ("IMG"); Foreign Currency ($ IGElement as $ imgement) {$ imgSrc = imagecreatefromstring (file_gate_content ($ imgElement-> Millatact ("src"))); If (imagesx ($ imgSrc)> 740) {// We change the IMAG tag with a new IMG, the desired width is $ newE = $ dom-> Create Element ('IMG'); $ NewE- & gt; Set attribute ('width', 740); $ NewE- & gt; Set property ('src', $ imgElement-> getAttribute ("src")); // $ imgElement-> instead of the original IMG tag ParentNode-> Replaceable children ($ newE, $ imgElement); }} // html "resized" images with $ dcho- $ dom- & gt; Save HTML ();
Comments
Post a Comment