Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I was able to come up with a time-consuming fix to this. Do the images being stored <em>need</em> to be stored with those black borders? It'd be much better if you could run every image with the black borders through the following script (using php to loop through every image in the directory) and let php override the old, black-bordered image with the new, borderless image.</p> <p>The approach I took was to create 4 loops:</p> <ol> <li>To look at black borders on the right (loop through x -> loop through y)</li> <li>To look at black borders on the left (loop through x -> loop through y)</li> <li>To look at black borders on the bottom (loop through y -> loop through x)</li> <li>To look at black borders on the top (loop through y -> loop through x)</li> </ol> <p>Now, each of these loops had another loop in them which would loop through the other coordinate (ie., x->y or y->x). If the inner loop found that one of the pixels lying on the outer loop's line wasn't black, it broke the whole look. If it didn't find that, it would increase one to the counter.</p> <p>At the end, we simply create a new image with the new dimensions and copy from the new to the old one.</p> <pre><code>&lt;?php $image_path = "jcMHt.jpg"; $jpg = imagecreatefromjpeg($image_path); $black = array("red" =&gt; 0, "green" =&gt; 0, "blue" =&gt; 0, "alpha" =&gt; 0); $removeLeft = 0; for($x = 0; $x &lt; imagesx($jpg); $x++) { for($y = 0; $y &lt; imagesy($jpg); $y++) { if(imagecolorsforindex($jpg, imagecolorat($jpg, $x, $y)) != $black){ break 2; } } $removeLeft += 1; } $removeRight = 0; for($x = imagesx($jpg)-1; $x &gt; 0; $x--) { for($y = 0; $y &lt; imagesy($jpg); $y++) { if(imagecolorsforindex($jpg, imagecolorat($jpg, $x, $y)) != $black){ break 2; } } $removeRight += 1; } $removeTop = 0; for($y = 0; $y &lt; imagesy($jpg); $y++) { for($x = 0; $x &lt; imagesx($jpg); $x++) { if(imagecolorsforindex($jpg, imagecolorat($jpg, $x, $y)) != $black){ break 2; } } $removeTop += 1; } $removeBottom = 0; for($y = imagesy($jpg)-1; $y &gt; 0; $y--) { for($x = 0; $x &lt; imagesx($jpg); $x++) { if(imagecolorsforindex($jpg, imagecolorat($jpg, $x, $y)) != $black){ break 2; } } $removeBottom += 1; } $cropped = imagecreatetruecolor(imagesx($jpg) - ($removeLeft + $removeRight), imagesy($jpg) - ($removeTop + $removeBottom)); imagecopy($cropped, $jpg, 0, 0, $removeLeft, $removeTop, imagesx($cropped), imagesy($cropped)); header("Content-type: image/jpeg"); imagejpeg($cropped); //change to `imagejpeg($cropped, $image_path);` to save imagedestroy($cropped); imagedestroy($jpg); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload